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
Related
I'd like to know if there is a way that I can let a program execute int f = 37; Console.Beep( f, 10000 ) and while it plays, change the f variable so the sound actually changes.
EDIT: I don't want to use a loop which increases f because that'd make the sound stop playing for a little time (each iteration). And I know there are other ways, but I'd like to know about the Beep method.
If you want to play sounds in your console application and have a much wider control over what's being played, I suggest you to use the SoundPlayer class (MSDN reference here, give it a look to have an overview of its features).
The cons is that you have to create a wav file that produces the beep sound (it shouldn't be hard to find one googling a little bit) and a wav file for every different sound you want to implement in your application. You can put the files in your application directory, but you can also embed them as resources in your assembly:
private static SoundPlayer s_Beep;
private static SoundPlayer s_Buzz;
public static void Main(String[] args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
s_Beep = SoundPlayer(assembly.GetManifestResourceStream("YourNamespace.Beep.wav"));
s_Beep.Load();
s_Buzz = SoundPlayer(assembly.GetManifestResourceStream("YourNamespace.Buzz.wav"));
s_Buzz.Load();
s_Beep.PlayLooping();
Thread.Sleep(100);
s_Beep.Stop();
s_Buzz.Play();
// ...
}
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 playing an Audio use Windows Media Player in c# WinForms. I want to display a message at the end of the audio play back.
I have a separate Audio class for playing audio and in the play method I have written:
Player = new WMPLib.WindowsMediaPlayer();
public static void play()
{
Player.controls.play();
Player.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
}
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
isComplete=true;
}
public static boolean hasCompleted()
{
return isComplete;
}
here isComplete is a boolean variable initialized to false
In my form, the code for my play button is:
//Play my audio
while(!hasCompleted());
//display message
The problem is that when i click the play button, my application goes into an infinite loop.
However when i do this:
while(!hasCompleted())
MessageBox.Show("Playing");
//display message
It works fine.
Why is this happening?
I dont want to display a message while it's playing.
I tried using:
Player.currentMedia.duration
and duration string property for a timer or Thread.sleep application but the value returned by both is always 0.
I also tried giving a delay of 1-2 seconds before calling the duration property but this doesn't work as some of my audio tracks are just 2 seconds long.
It is not the best way to do so since you are in a busy waiting situation which waste a lot of CPU cycles and may not work as you wish. I would recommend you to use "Event". Here you can find a very basic example:
http://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
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.
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.