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
Related
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.
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 am developing an app in wp7 and in some part.
I start a mediaelement to play my music file from isolated storage, with available functions(play, pause, next, previous, seek) and all goes perfect!
Now i want to make the app continue playing on user-exit or page-unload or something like that when mediaelement already playing!
Notice in the abobe try the
list.playlist: is a list with audiotracks
num_input: is the number of music file in list that will play, (get: from mediaelement)
time_input: is the timespan from mediaelement before stop, (set: backgroundaudioplayer to start)
play: is boolean variable, true if mediaelement plays
Stop(): is function that stops mediaelement from playing
Thats i am trying is:
private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
if (play == true)
{
TimeSpan time_input = med.Position;
Stop();
BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Track = list.playlist[num_input];
BackgroundAudioPlayer.Instance.Position = time_input;
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState) { BackgroundAudioPlayer.Instance.Pause(); }
else { BackgroundAudioPlayer.Instance.Play(); }
}
}
void Instance_PlayStateChanged(object sender, EventArgs e)
{
switch (BackgroundAudioPlayer.Instance.PlayerState)
{
case PlayState.Playing:
break;
case PlayState.Paused:
case PlayState.Stopped:
break;
}
}
The problem is that when the app page-unload (not on user exit, the first problem)
the backgroundaudioplayer gets the name and the EnabledControls and all staff but it isn't playing even I press the play button(so buttons not working, second problem)!
Also i want to control the list of my app beside that player(previous, next, play, pause) even the app is closed!
All the songs are located in isolated and i have the song-name and file-name in database. The audio track looks like:
AudioTrack audiotrack1 = new AudioTrack(new Uri(emp.EmployeeFile + ".mp3", UriKind.Relative), emp.EmployeeName, null, null, null, null, EnabledPlayerControls.All);
Thanks!
Implementing background audio requires more work. See this article for a walkthrough: http://msdn.microsoft.com/EN-US/library/windowsphone/develop/hh202978(v=vs.105).aspx
Beware the agent lives in another process, I'm not sure whether SqlCe supports concurrent access to the same database from different processes. IMO the best way to communicate with the background agent is the isolated storage file (e.g. the playlist) guarded by the named System.Threading.Mutex.
So, I'm trying to do a simple GUI to make music with Beeps on C#. I've been trying but I'm not sure if it's even possible to make a Console.Beep play as I hold down a button for example.
There's the normal Beep() method that just plays a short beep with a medium frequency and there's an overload Beep(int frequency, int duration). What I want to do is actually play it the whole time I'm holding the button, but obviously I can't previously state the duration.
I'm thinking this isn't possible but maybe there is a way?
This is also my first question on the site, so, hey.
You could do it like this, i just tested it and it works, and does not lock up the form while running.
private void Window_MouseDown_1(object sender, MouseButtonEventArgs e)
{
// Starts beep on background thread
Thread beepThread = new Thread(new ThreadStart(PlayBeep));
beepThread.IsBackground = true;
beepThread.Start();
}
private void PlayBeep()
{
// Play 1000 Hz for max amount of time possible
// So as long as you dont hold the mouse down for 2,147,483,647 milliseconds it should work.
Console.Beep(1000, int.MaxValue);
}
private void Window_MouseUp_1(object sender, MouseButtonEventArgs e)
{
//Aborts the beep with a new 1ms beep on mouse up which finishes the task.
Console.Beep(1000, 1);
}