Hi everyone, i have some question regarding my project.. I am currently work on a MP3 player project.. And i came out with a problem..

Here is my code

private void mediaPlayer_PlayStateChange_1(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    if (e.newState == 3)
    {

        double dur = mediaPlayer.currentMedia.duration;

        currentSongProgress.Maximum = (int)dur;

    }
    if (e.newState == (int)WMPPlayState.wmppsPlaying)
    {
        StatusText.Text = "[Playing]";
        timer2.Start();
        btnPlay1.Enabled = false;
        currentSong.Text = mediaPlayer.currentMedia.name;
    }

    else if (e.newState == (int)WMPPlayState.wmppsPaused)
    {
        if (!finished)
        {
            StatusText.Text = "[Paused]";
            timer2.Stop();
            btnPlay1.Enabled = true;
            currentSong.Text = mediaPlayer.currentMedia.name;
        }
    }

    else if (e.newState == (int)WMPPlayState.wmppsStopped)
    {
        StatusText.Text = "[Ended]";
        timer2.Stop();
        btnPlay1.Enabled = true;
        currentSong.Text = mediaPlayer.currentMedia.name;
    }

    else if (e.newState == (int)WMPPlayState.wmppsMediaEnded)
    {
        StatusText.Text = "[Ended]";
        finished = true;
        timer2.Stop();
        btnPlay1.Enabled = true;
        currentSong.Text = mediaPlayer.currentMedia.name;
        playlist2.SelectedNode = playlist2.SelectedNode.NextNode;
        mediaPlayer.Ctlcontrols.play();
        timer2.Start();
    }

}

this code should change my treeview node and play the next song when the current song ended..

else if (e.newState == (int)WMPPlayState.wmppsMediaEnded)
{
    StatusText.Text = "[Ended]";
    finished = true;
    timer2.Stop();
    btnPlay1.Enabled = true;
    currentSong.Text = mediaPlayer.currentMedia.name;
    playlist2.SelectedNode = playlist2.SelectedNode.NextNode;
    mediaPlayer.Ctlcontrols.play();
    timer2.Start();
}

But somehow, that code doesn't help me in automatically change song at all.. Anyone can help? Oh.. Anyway, i am using treeview toolbars as my playlist.. :) :Cheer

Dani AI

Generated

A couple of things to check based on what you described. First, make sure that changing the selected node actually tells Windows Media Player which file to play. As hinted, simply selecting a TreeView node does nothing unless you set mediaPlayer.URL (or currentPlaylist/currentMedia) somewhere. Route the "what to play" decision through the TreeView, then let your PlayState handler only decide "when to move to the next item." Also, prefer NextVisibleNode over NextNode so nested nodes are handled, and reset any flags (like finished) when you start the next track. is right: drop a breakpoint or log the state values to confirm you actually get wmppsMediaEnded (8) and in what order other states fire.

Wire up the TreeView to load and start a track whenever selection changes:

private void playlist2_AfterSelect(object sender, TreeViewEventArgs e)
{
    var path = e.Node.Tag as string;
    if (!string.IsNullOrEmpty(path))
    {
        finished = false;
        mediaPlayer.URL = path;          // point WMP at the chosen file
        mediaPlayer.Ctlcontrols.play();  // start playback
        currentSong.Text = e.Node.Text;
        btnPlay1.Enabled = false;
    }
}

Then keep the PlayState handler focused on advancing the selection when a track ends:

private void mediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
    {
        var next = playlist2.SelectedNode?.NextVisibleNode ?? playlist2.Nodes[0];
        if (next != null) playlist2.SelectedNode = next; // AfterSelect will set URL and play
        else btnPlay1.Enabled = true; // nothing left to play
    }
}

Quick troubleshooting tips:

  • Log e.newState to verify the sequence and avoid UI updates on wmppsStopped that can follow wmppsMediaEnded.
  • Guard against NextVisibleNode == null (end of list) or wrap to the first playable node.
  • Do not re-enable the Play button while you are auto-advancing.

Recommended Answers

All 2 Replies

I never use windows media player active x controls, so im not 100% certain.

course if you are starting the next song, why do you enable the play button?

What is the PlayList2 ? does changing its node set the mediaplayer currentmedia?
if not, then you need to set the current media of the mediaplayer to the nodes's value.

For any further assistance we need more code. I can't see the whole picture here.

Have you tried inserting break points? Does the code get run at all? As with DiamondDrake, we need more information about what your playlist and playlist2 are and how they interact and why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.