Is there a way to play more than one sound while let's say the background music is going?
The way I have it is:
public static void PlaySound(string filename)
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = filename;
player.Play();
}
Which will play one sound, but using the same "PlaySound" it will basically stop the last sound played and just start a new thread playing the new sound. Is there a way to actually stop this and play more than one sound(s)?
Take a look at:
Play multiple sounds using SoundPlayer
n-c-net
Note the solution with mciSendString at the bottom.
Related
First of all, My music is .wav file and I'm sure of it; I want to use it as a background music but it doesn't work!
even my sound system windows telling me this form you actually created has no sound.
also I tried these codes below but didn't work!
using (var player = new SoundPlayer("F:\\youtube\\Music\\fl.wav"))
{
// Use PlaySync to load and then play the sound.
// ... The program will pause until the sound is complete.
player.PlaySync();
}
SoundPlayer soundPlayer = new SoundPlayer("F:\\youtube\\Music\\fl.wav");
soundPlayer.Play();
My biggest problem is that it is only played once in the end of the loop. I was wondering if there is a Pause for loop to play a sound and then continue the loop but I can't find anything like this.
class Feladat
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
int seconds = 3;
public void Udv()
{
player.SoundLocation = "./sounds/type2.wav";
Console.WriteLine();
string szoveg = "Üdvözöllek !";
for (int i = 0; i < szoveg.Length; i++, System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.050)))
{
string current = Convert.ToString(szoveg[i]);
System.Console.Write(current);
player.Play();
}
}
}
I had to Cut off the start of the .wav file, because it was too long for the play intervall. Problem Solved !
From the docs:
The Play method plays the sound using a new thread. If you call Play before the .wav file has been loaded into memory, the .wav file will be loaded before playback starts. You can use the LoadAsync or Load method to load the .wav file to memory in advance. After a .wav file is successfully loaded from a Stream or URL, future calls to playback methods for the SoundPlayer will not need to reload the .wav file until the path for the sound changes.
Seems loading is not done in time your loop iterates again.
Try moving 'System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.050))' from the loop to after player.Play(); and tell us if it helps.
This question already has answers here:
How to use System.Media.SoundPlayer to asynchronously play a sound file?
(2 answers)
Closed 5 years ago.
I've been trying to get this button to do what I want for a little now. I want it to play a sound when I press it, which is what I managed to do. However, its like the application freezes every time you press the button, giving the sound all of its attention, etc. So basically my goal is to make the button play a sound without making the UI have to stop and allow it to play, before moving on. I also would like to know if there is a way to make a button play sound when pressed, but when pressed again the current sound is stopped and plays again, to prevent it from playing "X" amount of clicks you clicked the button, etc.
Here is my code:
public static void ButtonSound()
{
SoundPlayer _sound = new SoundPlayer();
try
{
_sound.Stop();
_sound.SoundLocation = path + "ButtonTap.wav";
_sound.Load();
_sound.PlaySync();
}
catch
{
}
finally
{
_sound.Dispose();
}
}
And Button code:
private void Button()
{
SoundPlayers.ButtonSound();
}
Note, I have my SoundPlayer in another class, and I am using DelegateCommands to reference my Button() method, etc. I am also using .wav files. Also, is there a more efficient way to achieve that task I am trying to accomplish? If you need anything else, just ask.
Thanks!
Your UI is freezing because that is what you are asking for when you call _sound.PlaySync().
Looking at the SoundPlayer documentation, you could use the Play() method:
Plays the .wav file using a new thread, and loads the .wav file first
if it has not been loaded.
Using this method should also solve the problem of playing the sound repeatedly, since you will no longer be queuing your calls.
Also, what you did there with Try-Finally-Dispose can be simplified with using, so your code would look like this:
public static void ButtonSound()
{
using (var _sound = new SoundPlayer())
{
_sound.SoundLocation = path + "ButtonTap.wav";
_sound.Play();
}
}
Note that your _sound.Stop() doesn't make sense, since you are calling it on a new object, but just calling Play() on a new object will make the old sound stop and then play the new one.
Also, the doc says that Play() loads the file if it has not been loaded, that is why I skipped the _sound.Load() call.
I'm programming a game and using NAudio with WaveOut. The sound is played when a collision occurs. The problem is the sound plays some times and after a few seconds if the collision happens again, the sound isn't played. I'm using this code.
WaveOut device = new WaveOut();
WaveStream source = new Mp3FileReader("hit.mp3");
device.Init(source);
And to play it,
source.Position = 0;
source.CurrentTime = TimeSpan.Zero;
device.Play();
Am I forgetting anything?
Thanks.
Try take a look at this instruction, by Mark Heath, on how to use NAudio for "fire and forget" audio playback:
http://mark-dot-net.blogspot.co.uk/2014/02/fire-and-forget-audio-playback-with.html
I've a really simple method for playing sound effects:
private void PlaySound(string file)
{
var sp = new SoundPlayer(#"Effects\" + file + ".wav");
sp.Play();
}
Then I do this to call it:
PlaySound("music");
Now, the first time PlaySound("music") gets called, it won't play it. The second time and all the other times after that it will.
Any ideas of what goes wrong here?
Try this:
private void PlaySound(string file){
using (SoundPlayer player = new SoundPlayer(#"Effects\" + file ' ".wav"))
{
// Use PlaySync to load and then play the sound.
player.PlaySync();
}
}
Why use PlaySync? If you just call the Play method in this program, the program will terminate before the sound plays. The Sync indicates that the program should pause while the sound plays.
You need to call the load method before playing.If the the file is not already loaded ,the file will be loaded with a call to Play.Which explains why the file is not played the first time.
If you call Play before the .wav file has been loaded into memory, the .wav file will be loaded before playback starts.-MSDN
Both Load and PlaySync will block the current thread.A better option would be to use the LoadAsync to load the file asynchronously.