C# SoundPlayer Question - c#

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.

Related

Playing sound from Database using MediaPlayer C#

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.

Monotouch sound doesn't play properly

I'm trying to play a sound file in monotouch but it gets cut off after a second or two. Here is the code I'm using to play the sound:
SoundFileName = filename;
var sound = SystemSound.FromFile(filename);
sound.PlaySystemSound();
It's an MP3 file that I'm trying to play. Again, I hear it for a brief second and then it gets cut. I added a thread.sleep() line afterwards and THEN it'll play throughout.
But that's not ideal because the length of the mp3 files that I'm playing could vary. Any help would be greatly appreciated. Thank you.
You need to declare your sound object at the form or class level. In your posted sample, sound is only scoped to the function, so as soon as the method ends the variable goes out of scope and is disposed before the sound finished playing. Thread.sleep works because that call prevents the method from ending for a while (and thus prevents sound from going out of scope).
You may also want to look at this question: Playing a sound with MonoTouch
I don't think SystemSound is intended to use with MP3's of open duration.

Playing 2 sounds together at same time

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");

Play simultaneously sound in C#

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 ?

Mediaplayer in c# windows application

I have added and mediaplayer control to my c# application, and I can make it play a .wma song. My problem is that i want to see if the player is playing. Is there an event that I can use? Or is it a way I can see for how many seconds a song has been playing?
Yes, you can check the PlayState property or handle the PlayStateChange event.

Categories

Resources