I am trying to play multiple sounds at the same time for a simple game using the MediaPlayer class (System.Windows.Media.Mediaplayer).
To play the same sound multiple times at the same time, I created my own class, which loads the same sound many times.
Here is a short example:
sound = new MediaPlayer[count]; //array of MediaPlayers
//...
for (int a = 0; a < count; a++)
{
sound[a] = new MediaPlayer();
sound[a].Open(new System.Uri(sp + pfad)); //load sound
}
Here is my problem:
If I try to play a sound shortly after starting the game either I can't hear the sound or the beginning of the first sound I loaded will start playing (even if I tried to play a completely different sound).
It seems like all these MediaPlayers (I'm loading ~100) need some time to load the sounds.
Therefore I'm looking for a way to check if the loading is done.
Loading or playback? When the media has finished playback it rises the MediaEnded event, when buffering has finished it rises BufferingEnded event. MediaPlayer events.
Related
I am creating a morse code interpreter on C# because I am bored.
I have the whole thing working except one thing, the beep.
I am currently using Console.Beep(1000, timer1.Interval); in the timer1_tick event to beep for the same amount of time as the timer ticks, attempting to emulate a constant beep until the key is released. However this comes out very choppy with a few ms gap of silence between beeps.
Is there any way to create a constant buzz/beep sound until my isPressed bool is found false in the tick event?
Why you don't use playing the wav file?
You can have a special file and play it, then stop playing when the key is released...
//start
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = #"file.wav";
player.Play(); //or player.PlaySync();
//stop
player.Stop();
Hope this helps!
I have this problem where I can't stop the audio file from playing.
This is a snippet of my code:
private Sound bgMusic;
bgMusic = new Sound("bgMusic.wav");
if(gameHasStarted)
bgMusic.Play();
else
bgMusic.Stop();
I've searched Google mulptiple times, but without any success. The music just doesn't stop.
I've tried threading this audio file, but without luck.
I've also tried using a MediaPlayer - the sound always plays, whichever option I choose, but it just doesn't stop.
So my question:
How do I stop audio file from playing? Do I need to reset it? Close it? Destroy it? How?!
It's actually not that difficult:
private System.Media.SoundPlayer playerMainMenu; - initialize player
playerMainMenu = new System.Media.SoundPlayer("getlucky.wav"); - load file
playerMainMenu.PlayLooping(); - to play looping
playerMainMenu.Play(); - to play once
playerMainMenu.Stop(); - to stop this player
Works like a charm. Found by trial-and-error.
I am traversing an array list of Music Notes object with the aim to play a whole stream of sound files all of each with a different duration.
The problem is that I can hear only just like a beep for each music note!!
The thread is going sleep for the length of the current MusicNote so I don't know why I am hearing only beeps!
//----------Play the whole list----------
public void PlayAll()
{
foreach (MusicNote m in list)
{
m.sp1.Stop();
m.sp1.Play();
Thread.Sleep(m.NoteDuration * 100);
}
}
Your posted code should work.You will just call that function on thread. if console there is no need for thread too
And here is your problem :
If the .wav file has not been specified or it fails to load, the Play
method will play the default beep sound.
SoundPlayer.Play documentation
Heey,
We created a game with Monogame but we got the following problem.
We got a themesong that plays when you have loaded the game now is the problem that the themesong sometimes plays but sometimes just doesn't. We convert it by the XNA pipeline to a wma and load it into our game with the .xnb together but just sometimes the music doesn't wanna start.
We just use the standard code for starting a song and all of this code does fire.
internal void PlayBackgroundMusic()
{
if (MediaPlayer.GameHasControl)
{
MediaPlayer.Volume = mainVolume;
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
}
}
We also use SoundEffects but these work 100% of the time it's only the song that won't play everytime you start. Windows RT runs it fine by the way.
Make sure that the debugger gets into the if statement through debugging (or remove the statement temporarily). Another possibility might be that the function is running before the game is fully initialized. You could try delaying the function until the game has been fully loaded.
PS: I can't comment on questions yet so here's an answer.
Edit:
Alright, after some messing around with the Song class and looking in the implementation in MonoGame I came to the conclusion that the SoundEffect class is easier to use and better implemented.
backgroundSound = Content.Load<SoundEffect>("alarm");
protected override void BeginRun()
{
// I created an instance here but you should keep track of this variable
// in order to stop it when you want.
var backSong = backgroundSound.CreateInstance();
backSong.IsLooped = true;
backSong.Play();
base.BeginRun();
}
I used this post: using BeginRun override to play the SoundEffect on startup.
If you want to play a song, use the Song class. But make sure you are using ".mp3" instead of ".wma" before converting them into ".xnb".
backgroundMusic = Content.Load<Song>("MainTheme");
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
See MSDN.
I know I can reference XNA for the SoundEffect class and that's what I've been doing so far but I was wondering if there was a better way than what I've been doing.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using (var stream = TitleContainer.OpenStream("test.mp3"))
{
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
For my test app I have 20 sounds each 1 second long that I want to play once button are pressed. I'm playing around with different techniques but if possible I'd like to know how professionals go about doing this before I commit in making a sound effect based app. Little things such as loading the sound effect first or loading it the instance the button is pressed would be helpful.
Thanks.
If I were you I would use PhoneyTools SoundEffectPlayer
This class is used to play SoundEffect
objects using the XNA integration. The
player must live long enough for the
sound effect to play so it is common
to have it scoped outside a method.
For example:
public partial class MediaPage : PhoneApplicationPage
{
// ...
SoundEffectPlayer _player = null;
private void playButton_Click(object sender, RoutedEventArgs e)
{
var resource = Application.GetResourceStream(new Uri("alert.wav", UriKind.Relative));
var effect = SoundEffect.FromStream(resource.Stream);
_player = new SoundEffectPlayer(effect);
_player.Play();
}
}
I think a good example would be the official sample on AppHub. It demonstrates how to play multiple sounds. You can directly download the sample from here.
This sample demonstrates how to use
the XNA Framework's SoundEffect and
SoundEffectInstance classes to play
multiple sounds simultaneously in a
Silverlight application for Windows
Phone. It also shows a simple way to
set up a DispatchTimer to call
FrameworkDispatcher.Update in order to
simulate the Game loop for the XNA
Framework's internals. Finally, it
shows how to load a wave audio file
into a Stream that can be played by
the SoundEffect classes.