Results 1 to 2 of 2

Thread: How to play an Audio File After another has Finished in WPF MediaElement

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    830

    How to play an Audio File After another has Finished in WPF MediaElement

    Good Day

    i have a Media Element in WPF and i am playing mp3's programatically on a click of a button. Now there are times where i want to Play two different mp3's in an order after another e.g


    * *
    Code:
    *
                        PlayAudio("AccountOpen_Message1"); 
     
                        PlayAudio("AccountOpen_Message2");
    Now i tried to put it a Thread Sleep between, it work once after that the Second mp3 plays after the first one. So i want to call the same function twice to play different mp3's but i want one to wait for another to finish playing before playing, i hoped for a "isPlaying" Property to determine if the element was playing. Does anyone have a solution.





    Code:
         //Function to Play a Video
            private void PlayAudio(string Fruit)
            {
                VideoPlayer.Source = new Uri(@"D:\Articles\How to identify Players in Kinect\IdentifyPlayers\WpfApplication1\WpfApplication1\Voices\" + Fruit + ".mp3", UriKind.Absolute);
                VideoPlayer.LoadedBehavior = MediaState.Manual;
                VideoPlayer.Play();
            }
    Code:
     <MediaElement x:Name="VideoPlayer"  Volume="100" LoadedBehavior="Manual" MediaEnded="VideoPlayer_MediaEnded"  UnloadedBehavior="Close"   ></MediaElement>

    Code:
       private void VideoPlayer_MediaEnded(object sender, RoutedEventArgs e)
            {
                this.Close();
            }



    Thanks

  2. #2
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: How to play an Audio File After another has Finished in WPF MediaElement

    The simpliest way I can think of as of now:

    Code:
    //...
    
    private Queue<Uri> playList = new Queue<Uri>()
    
    
    private void button1_Click(object sender, RoutedEventArgs e)
    {
       playList.Enqueue(new Uri(@"C:\temp\mysound_1.mp3"));
       playList.Enqueue(new Uri(@"C:\temp\mysound_2.mp3"));
    
       PlayAudioPlaylist();
    }
    
    private void PlayAudioPlaylist()
    {
       if (playList.Count > 0)
       {
          mediaElement.Source = playList.Dequeue();
          mediaElement.LoadedBehavior = MediaState.Manual;
          mediaElement.Play();
       }
    }
    
    private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
    {
       PlayAudioPlaylist();
    }
    
    //...
    That said, you could make it a whole lot prettier if you wrap it in its own class or create a new control and inherit from the MediaElement with a few modifications that exposes an IsMediaPlaying property.
    Last edited by wakawaka; Jan 10th, 2013 at 09:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width