I want in the win Application (written in C#), the sound (Wav Format) play as Background Sound, and mouse over Control play small wav sound File,
and when Click on Button, Stop Background Sound and ... .
Thanks For your guidance.
You could try look around in the System.Media namespace. There is a SoundPlayer which is able to play Wave files.
To play a wav file in a loop, you can use the following code:
string filename = #"C:\WINDOWS\Media\notify.wav";
System.Media.SoundPlayer player = new System.Media.SoundPlayer(filename);
player.PlayLooping();
To stop playing, you simple call Stop():
player.Stop();
Play around a bit, there's more if you need it.
you can use Windows Media Player:
reference C:\Windows\System32\wmp.dll in your project
to launch a wav file:
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayerClass();
wmp.URL = "[wav file path]";
//then control the player with :
wmp.controls.play(), stop(), ...
for the second wav file, do the same thing with another instance of WindowsMediaPlayer etc...
you can also use Managed DirectX : Managed DirectX Tutorial Part 2
Did you try to play each of them in a separate thread ?
Related
I want to play .wav sound in my application. I know how to do it. For example with a SoundPlayer class:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"C:\path\sample.wav";
player.Load();
player.Play();
At home I have multiple Applications which can play sounds. However at job, I can't do it. Normally the computer can make sounds (youtube, Windows Media Player, etc.).
I investigated that my application does not appear in Volume Mixer.
In the end it was faulty wav file. I converted it to mp3 and then back to wav and now it works.
I have sound stored in database in byte[] type. I can easily play with SoundPlayer, but not with MediaPlayer. Nice thing about MediaPlayer is I can play from middle of the sound. SoundPlayer doesn't have this feature. Is there way to play sound which stored in database with MediaPlayer(not from the filesystem).
Any help appreciated.
Unfortunately the MediaPlayer class can only Open media from the file system - it's simply a limitation. It's only option is to receive a Uri. However, I would recommend leveraging NAudio. With NAudio you can in fact play, pause, stop, rewind, and more all in memory.
I'm trying to get one .wav file to play as ambient music though when another sound is played, it halts the ambient music and I've tried doing play-loop and tried the various sync, async , load.
I think I'm missing something here. Thank you in advance.
Check this question on SO and the suggestions made there. The basic problem is that SoundPlayer can only play one sound stream at a time - if you want to play multiple sounds concurrently you need a different solution.
It is possible to use Windows Media Player for the ambient sound, and play another sound with SoundPlayer at the same time:
// example begin
WMPLib.WindowsMediaPlayer mediaPlayer = new WMPLib.WindowsMediaPlayer();
mediaPlayer.URL = "<theAudioFile>";
mediaPlayer.settings.setMode("loop", true);
mediaPlayer.controls.play();
// example end
A reference to Windows Media Player assembly needs to be added to the solution as well.
Designing a game for any laptop:
I want to play 2 sounds at the same time! A background music and a sound when pressing on a button!
Using System.Media won't allow me to play 2 sounds together. For background music, I used the following:
SoundPlayer sp = new SoundPlayer(#"..\..\bin\debug\tribal dance.wav");
sp.Play;
If I played another SoundPlayer, the previous one stops!
I want the background music to keep playing until the form is closed! And at the same time, if a button is clicked, another sound will be played with the music!
I also tried added the Windows Media Player to the toolbox and used it (allowing me 2 play 2 sounds at same time). That works fine but the path will give an error if I put it (#""..\..\bin\debug\tribal dance.wav");
and I need to put it that way in order to be read by any computer.
Any suggestions?
You CANNOT play two sounds at once using SoundPlayer.
SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.
There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)
The simplest option would be to depend on windows media player to play the audio for you.
Add a reference to "C:\Windows\System32\wmp.dll"
then use
var player = new WMPLib.WindowsMediaPlayer();
player.URL = #"..\..\bin\debug\tribal dance.wav";
Note: play starts immediately after setting the URL property
The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.
Using (WMPLib) Windows Media Player Library you can do this easily. Keep Your file in Debug/Sound folder and follow the following code. This will run 2 file simultaneously. To Add WMPLib in your project add it by NuGet Package Manager.
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
WMPLib.WindowsMediaPlayer wplayer2 = new WMPLib.WindowsMediaPlayer();
string play_string = "be.mp3";
wplayer.URL = #"SOUND/" + play_string;
wplayer.controls.play();
string play_string2 = "ba.mp3";
wplayer2.URL = #"SOUND/" + play_string2;
wplayer2.controls.play();
http://msdn.microsoft.com/en-us/library/dd562851%28v=VS.85%29.aspx this article may help
If you need absolute path to file you can use next code
string soundFile = Path.Combine(Directory.GetCurrentDirectory() + "file.wav");
how do I write a simple code in C# that plays a mp3 file when i click a button.
If you System.Diagnostics.Process.Start the mp3 file by name, I would expect any associated player application - such as media player - to launch and play the file. Are you asking how to play an MP3 file without displaying a player?
You can use the Windows Media Player ActiveX control just fine.