11 Replies - 22458 Views - Last Post: 26 April 2011 - 07:45 AM Rate Topic: -----

#1 IAmCypher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 14-April 10

C# Media Player Automatically Play Next MP3

Posted 25 November 2010 - 01:30 PM

Hello DreamInCode,

I've been trying to get my mp3 player app to automatically play a new mp3 after it's finished his current one. However it doesn't work how I planned.

First here is my code:

using System;
using System.Windows.Forms;

namespace TestMp3
{
    public partial class Form1 : Form
    {
        private WMPLib.WindowsMediaPlayer wmp;
        public Form1()
        {
            InitializeComponent();
            wmp = new WMPLib.WindowsMediaPlayer();
            wmp.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(PlayStateChange);
            wmp.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(MediaError);
        }

        private void Play(string path)
        {

            wmp.controls.stop();
            wmp.close();
            wmp.URL = path;
            wmp.controls.play();
        }

        private void Next(string path)
        {
            button1.Enabled = false;
            Play(@"C:\xml\music\songs\bird.mp3");
        }

        private void PlayStateChange(int NewState)
        {
            if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
            {
                Next("");
            }
        }

        private void MediaError(object pMediaObject)
        {
            button1.Enabled = true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Play(@"C:\xml\music\songs\bee.mp3");
        }
    }
}


And here is what it does and what it doesn't do:

  • Start playing bee.mp3
  • Run the code in the play state changed (Next())
  • Disable the button and run Play method with bird.mp3
  • It runs all the code in the Play method
  • Goes to play state changed again (I have no idea why but it stops from playing) It doesn't play the second song.
  • Starts again the next method thus calling Play again on birds.mp3
  • Runs all the code in Play method
  • End of Line, nothing happens from here, Nothing is singing, nothing is happening anymore, no code is being executed it just goes idle.


Any idea?

Thank You,
IAmCypher

Edit: I forgot to add that I've read this thread and I couldn't find my answer.

This post has been edited by IAmCypher: 25 November 2010 - 01:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C# Media Player Automatically Play Next MP3

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# Media Player Automatically Play Next MP3

Posted 25 November 2010 - 02:45 PM

Does the PlayStateChange get called after a song?
Was This Post Helpful? 0
  • +
  • -

#3 IAmCypher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 14-April 10

Re: C# Media Player Automatically Play Next MP3

Posted 25 November 2010 - 02:55 PM

View Postmavarazo, on 25 November 2010 - 10:45 PM, said:

Does the PlayStateChange get called after a song?


Yes, exactly after the first song finished and for some reason right after the second song starts (or when it should start).
Was This Post Helpful? 0
  • +
  • -

#4 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# Media Player Automatically Play Next MP3

Posted 25 November 2010 - 11:44 PM

This event will always be fired, because the Player changes his state.
http://msdn.microsof...y/ms930667.aspx

So and next your if statement is true, when you expect to play the next song?
Was This Post Helpful? 0
  • +
  • -

#5 IAmCypher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 14-April 10

Re: C# Media Player Automatically Play Next MP3

Posted 26 November 2010 - 03:39 AM

View Postmavarazo, on 26 November 2010 - 07:44 AM, said:

This event will always be fired, because the Player changes his state.
http://msdn.microsof...y/ms930667.aspx

So and next your if statement is true, when you expect to play the next song?


Yes it does get called however i placed on line 34 an if statement to run the next method only if the state changed to a "stopped" one.
Was This Post Helpful? 0
  • +
  • -

#6 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# Media Player Automatically Play Next MP3

Posted 26 November 2010 - 07:13 AM

You didn't answer my question:

Quote

So and next your if statement is true, when you expect to play the next song?

Was This Post Helpful? 0
  • +
  • -

#7 IAmCypher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 14-April 10

Re: C# Media Player Automatically Play Next MP3

Posted 26 November 2010 - 10:24 AM

View Postmavarazo, on 26 November 2010 - 03:13 PM, said:

You didn't answer my question:

Quote

So and next your if statement is true, when you expect to play the next song?


I fail on understanding the question properly, You mean when I expect the next song to play? I want it to play exacly after the first one has ended!
Was This Post Helpful? 0
  • +
  • -

#8 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# Media Player Automatically Play Next MP3

Posted 26 November 2010 - 11:43 AM

Ok, sorry. What is the result of the IF statement?

if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)


Is it true or false?
Was This Post Helpful? 0
  • +
  • -

#9 IAmCypher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 14-April 10

Re: C# Media Player Automatically Play Next MP3

Posted 27 November 2010 - 05:00 AM

View Postmavarazo, on 26 November 2010 - 07:43 PM, said:

Ok, sorry. What is the result of the IF statement?

if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)


Is it true or false?


When the button1 click event fires the result of the if statement is false.
When the first song ends the result of the if statement is true.
After the code in Play method at the moment of the second song (after Next gets first called) the playstate event fires again (I have no idea why) and the result of the if statement is true again.
After the Play method gets called again (after Next gets called for the second time) the playstate event fires again and the result of the if statement is false.

Thank you for your time.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: C# Media Player Automatically Play Next MP3

Posted 27 November 2010 - 09:39 PM

It changes twice because the state changes twice.
Playing -> Stopped
Stopped -> Playing
Was This Post Helpful? 0
  • +
  • -

#11 IAmCypher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 14-April 10

Re: C# Media Player Automatically Play Next MP3

Posted 28 November 2010 - 03:53 AM

View PostAdamSpeight2008, on 28 November 2010 - 05:39 AM, said:

It changes twice because the state changes twice.
Playing -> Stopped
Stopped -> Playing


That is correct but the IF statement should be false if the state is change to playing. But after the next is changed my app tries to play another song but the state will change again in a stopped one!
Was This Post Helpful? 0
  • +
  • -

#12 bsee123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-April 11

Re: C# Media Player Automatically Play Next MP3

Posted 26 April 2011 - 07:45 AM

I also had this problem. I was designing a routine to play videos from a list in a store display. I believe that the reason a new song will not properly load has something to do with the fact that a new thread is created every time a change-state event is kicked off. When you add code that is executed from your event, it in turn kicks off another simultaneous event. All the threads colliding at once on the same resource causes the player to stop.

I worked around this by creating a timer that verifies the play state every few seconds, rather than worrying about events.

The gist of the routine that I got to work looks something like this, I have left out declarations but hopefully you can get the main idea of how this works through the snippet I added below:

'This handles the tick event that can occur at the time interval you select (ticks in this case are set in milliseconds)

Private Sub tmrStateChange_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrStateChange.Tick

   GetStateChange()

End Sub

'The subroutine can handle each special media player event individually with your custom code

Private Sub GetStateChange()


     Select Case wmpVideo.playState

         Case WMPLib.WMPPlayState.wmppsPlaying
              Exit Sub 'We are playing...just let it do it's thing

         Case WMPLib.WMPPlayState.wmppsStopped
             LoadNextVideo()
             wmpVideo.URL = CurrentVideo 'A string containing the file name for the next video
             System.Threading.Thread.Sleep(3000) 'After loading the new video, give it a chance to play before letting this routine finish
 
            Case Else
              System.Threading.Thread.Sleep(3000) 'Just wait a little before continuing
             Exit Sub

     End Select

        'Other possible play states that can occur
        'Case WMPLib.WMPPlayState.wmppsBuffering
        'Case WMPLib.WMPPlayState.wmppsLast
        'Case WMPLib.WMPPlayState.wmppsPaused
        'Case WMPLib.WMPPlayState.wmppsReady
        'Case WMPLib.WMPPlayState.wmppsReconnecting
        'Case WMPLib.WMPPlayState.wmppsScanForward
        'Case WMPLib.WMPPlayState.wmppsScanReverse
        'Case WMPLib.WMPPlayState.wmppsTransitioning
        'Case WMPLib.WMPPlayState.wmppsUndefined
        'Case WMPLib.WMPPlayState.wmppsWaiting

 End Sub



This post has been edited by JackOfAllTrades: 26 April 2011 - 05:22 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1