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
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.
dso = new DirectSoundOut(Guid.Parse(AudioOutDevice));
var ms = new MemoryStream(soundArray.ToArray()))
{
IWaveProvider provider = new RawSourceWaveStream(ms, new WaveFormat());
dso.Init(provider);
dso.Play();
Thread.Sleep(3000);
}
I am able to play sound array through desired output device using the above code, and i am unable to hear the sound if there is thread.sleep. But I am unable to understand the reason for using thread.sleep. Can any one let me know the reason for thread.sleep()
The call to Play is not blocking. It simply starts playback. So you must keep dso alive until playback ends or you have stopped it manually.
You can use code like this if you want to block yourself (obviously only use this if your audio isn't infinitely long)
dso.Play();
while (dso.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(500);
}
dso.Dispose();
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.
I am playing with using TTS built into .NET 4 and want the speech to happen immediately, but am instead encountering a lag between when I call Speak and when I get the audio.
I am developing a simple count-down timer that calls off the last five seconds and completion (5... 4... 3... 2... 1... Done), but when the screen updates with the new time, the TTS lags behind, getting worse for every invocation. I tried using SpeakAsync, but this only made it worse. Currently, Speak is being called outside the UI thread (in the Timer Tick event handler).
Is there a way to minimize this lag, such as pre-computing the speech and caching it or creating some kind of special TTS thread?
I somehow read past the API call I needed at least a hundred times. I was looking for SpeechSynthesizer.SetOutputToWaveStream.
MemoryStream stream = new MemoryStream();
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SetOutputToWaveStream(stream);
synth.Play(text);
stream.Position = 0;
SoundPlayer player = new SoundPlayer(stream);
player.Play();
This code will use TTS to turn text into a WAV file that is streamed into stream. You need to reset the position of the MemoryStream so that when you create a SoundPlayer from it, it starts reading the stream from the beginning instead of the end. Once you have the SoundPlayer initialized, you can save it somewhere so you can play it later instantly instead of having to wait for the TTS to initialize and play the sound.