Weird AxWindowsMediaPlayer Problem

Jordy1212
Jordy1212 used Ask the Experts™
on
I'm getting a problem while trying to play the next song in a media player. Basicly when the media ends I want it to play the next song from a listbox but it's only part working. here is my code:

 Public Sub player_PlayStateChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange

        If Not MediaStopped Then

            If e.newState = 8 Then

                lstMusic.SelectedIndex += 1

                AxWindowsMediaPlayer1.URL = lstMusic.SelectedItem

            End If

        End If

Open in new window


It seems to select the next song when the media ends but it doesn't play the next song so I assume it has something to do with:      AxWindowsMediaPlayer1.URL = lstMusic.SelectedItem

But again! I've used to code for _next and it works fine! so what's the problem?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I've just found another weird problem

        [/code]If Not MediaStopped And e.newState = 8 Then

            lstMusic.SelectedIndex += 1

            AxWindowsMediaPlayer1.URL = lstMusic.SelectedItem

            MsgBox("test")

        End If[/code]

If you do it with the msgbox("test") then it'll play the next song like I want it to but If you close the msgbox then it'll stop playing again!

Author

Commented:
also found out that fastreverese doesn't work
Commented:
I could solve it using an asynchronous call (see code).

I used the wmppsMediaEnded, but it might also work with another event.

Regards, Jacco


public delegate void NewUrlDelegate(string Text);

        private void NewSong(string url)
        {
            axWindowsMediaPlayer1.URL = url;
        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (((WMPLib.WMPPlayState)e.newState) == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                this.BeginInvoke(new NewUrlDelegate(NewSong), new object[] { @"C:\Users\Public\Music\Sample Music\Sleep Away.mp3" });
            }
        }

Open in new window

Author

Commented:
Yes! This solved it thanks! I didn't think i'd get an answer :)

Author

Commented:
I forgot to add for anyone having a simlar problem then use this code

[code]  Private Sub NewSong(ByVal url As String)
        axWindowsMediaPlayer1.URL = url
    End Sub

    Public Delegate Sub NewUrlDelegate(ByVal Text As String)

    Public Sub player_PlayStateChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange

        If Not MediaStopped And e.newState = 8 Then
            If lstMusic.SelectedIndex = lstMusic.Items.Count - 1 Then
                lstMusic.SelectedIndex = 0
                Me.BeginInvoke(New NewUrlDelegate(AddressOf NewSong), New Object() {lstMusic.SelectedItem})
            Else
                lstMusic.SelectedIndex += 1
                Me.BeginInvoke(New NewUrlDelegate(AddressOf NewSong), New Object() {lstMusic.SelectedItem})
            End If

        End If[/code]

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial