3

ok i have question, i made this code to play axmediaplayer base on item listed on listbox. first i make this code to make a list using opendialog :

 private string[] files, path;
 private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            files = openFileDialog1.SafeFileNames;
            path = openFileDialog1.FileNames;
            for (int i = 0; i < files.Length; i++) {
                listBox1.Items.Add(files[i]);
            }
        }
    }

and then it play the music when the listbox index changed (when the item on the list box cliked) using this code :

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
}

it works fine, and then i want player to automove to the next song base on item on my listbox. with using events PlayStateChange, so i make this code

private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded) 
    {
         if(listBox1.SelectedIndex < files.Length - 1)
         {
            listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
         }
    }
}

the selected index change, but the player doesn't auto play the next song. i must click the play button manually in order to play the list. can anyone help me up?

Cero
  • 61
  • 1
  • 6
  • where in the listbox is the item that you are selecting..? how many URL's do you have to choose from.. what happens if you click on the last URL in the List.. do you get an Error..? if you are checking e.newState can you also get a e.SeletedItem it's Index listBox1.SelectedIndex = e.SelectedIndex; – MethodMan Feb 03 '12 at 15:15
  • @Scott - thx for ur commend, i get the listbox item using opendialog then convert the result into an array path(maybe i must also write how i get the listbox?). and yeah it got crash when the last list got selected i know this comming coz when u hit the last list + 1 you got nothing to play. and i not sure what ur last question means – Cero Feb 03 '12 at 20:52
  • just fix the last code, now it not crash when list hit the last item. – Cero Feb 04 '12 at 05:44
  • i find something, when i checking my if statement i use mbox to check neither the statement trigered or not(i put the mbox under listBox1.SelectedIndex = listBox1.SelectedIndex + 1;) so if the statement fire my mbox will pop up. and it is poping up when song ended, and then something happen the next song played like i wanted so, but when i close the mbox it stop again. weird -_- – Cero Feb 05 '12 at 05:52
  • are you sure that you don't have any other events wired up that could actually be triggering something.. also are there any setting is the MediaPlayer with in your media code that would be set for things such as shuffle, random, preview, or something that determines if some other event happens to pause the media player?? – MethodMan Feb 06 '12 at 14:03
  • im sure there nothing like that, but to be sure i make a new project to check that out. the new one only have 3 event which is PlayStateChange, listBox1_SelectedIndexChanged, and buttons event. and not interfere with setting for the media player setting(Just drag from tool box and let it as it be) and the same thing still happening. how bout this, the PlayStateChange event keep firing again and again due the playstate that change all the time. if this true is there any good event to replace playstatechange event? – Cero Feb 07 '12 at 15:05

2 Answers2

3

ok i found it, the solution is to add timer before playing the next song. first im adding timer, that shoud be timer1. and then i change playstate event to something like this :

private void axWindowsMediaPlayer1_PlayStateChange(object sender, axWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
        {
            timer1.Interval = 100;
            timer1.Enabled = true;               
        }            
     }

then on the timer i adding tick event, the tick event is something like this :

 private void timer1_Tick(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex < files.Length - 1)
        {
            listBox1.SelectedIndex++;
            timer1.Enabled = false;
        }
        else
        {
            listBox1.SelectedIndex = 0;
            timer1.Enabled = false;
        }            
    }       

now its work fine ^^

Cero
  • 61
  • 1
  • 6
0

Below functionality worked for me:

    private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
        {

            timer1.Interval = 100;
            timer1.Start();
            timer1.Enabled = true;   
            timer1.Tick += timer1_Tick;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        /// method to play video list items
        myFuntiontoPlayVideo();
        timer1.Enabled = false;
    }     
Anjan Kant
  • 4,090
  • 41
  • 39