Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having some problems with a media player that I'm making in C#, I think this will be the last problem..

I have two buttons: Play and Pause. When I click Play, a selected song from a listbox is being played. If I click Pause, the song pauses correctly. But if I click on Play button again to resume the song, it restarts the song I was playing.

I'm using:
C#
private void button3_Click(object sender, EventArgs e) //Play Button
       { 
          axWindowsMediaPlayer1.URL = mediaList[listBox1.SelectedIndex];
          if(axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
               {
                  axWindowsMediaPlayer1.Ctlcontrols.play();
               }else
               {
                  axWindowsMediaPlayer1.Ctlcontrols.pause();
               }
       }


mediaList is a list for saving the playlist on listBox1.

Thanks in Advance.
Posted
Updated 16-Jan-15 7:28am
v3
Comments
DamithSL 16-Jan-15 13:11pm    
can you update the question with full code of play and pause methods?
ChrisCreateBoss 16-Jan-15 13:12pm    
Yeah sure. Wait a sec...
ChrisCreateBoss 16-Jan-15 13:32pm    
Code Updated!

1 solution

Assume you want to toggle Play and Pause using button. In case of URL not set then only you need to set one. so you can check URL first as below
C#
//code 1
if(!string.IsNullOrEmpty(axWindowsMediaPlayer1.URL)
{
   // URL set, you can pause or play current video 
   // check below Code2
}else
{
   // need to set the url from your mediaList selected item and play the video from beginning 
}

if you already have URL set then your video may be paused then you need to play it otherwise you need to paused it
C#
//code2
if(axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused){
    // video paused, resume it  
    axWindowsMediaPlayer1.Ctlcontrols.play();
}else{
    // otherwise pause the video
    axWindowsMediaPlayer1.Ctlcontrols.pause();
}

full code:
C#
private void button3_Click(object sender, EventArgs e) 
       { 
          if(!string.IsNullOrEmpty(axWindowsMediaPlayer1.URL)
          {
             if(axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused){
                axWindowsMediaPlayer1.Ctlcontrols.play();
             }else{
               axWindowsMediaPlayer1.Ctlcontrols.pause();
             }
          }else
          {
             axWindowsMediaPlayer1.URL = mediaList[listBox1.SelectedIndex];
             axWindowsMediaPlayer1.Ctlcontrols.play();
          }
          
       }
 
Share this answer
 
Comments
ChrisCreateBoss 18-Jan-15 12:48pm    
Thanks for your solution, it helped me a lot. It did work very good.
DamithSL 18-Jan-15 21:51pm    
You are welcome! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900