0

I am trying to make a MP3 player with playlist functionality, I have googled everywhere, tried examples, but nothing works. Where the code freezes up at is when p[ListBox1.SelectedIndex + 1]; is called, and I have no idea why it locks up the program. I have tried making an array and selecting the next song through that, but that also didn't work.

I think this is the easiest way to do this. But it locks up the program for some reason.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;
using System.Collections;

namespace MusicPlayer
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string[] f, p;
    ArrayList playlist = new ArrayList();
    private int current_index = 0;

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();

        if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){
            f = openFileDialog1.SafeFileNames;
            p = openFileDialog1.FileNames;

            for (int i = 0; i < f.Length; i++)
            {
                listBox1.Items.Add(f[i]);
            }

            foreach (string d in open.FileNames)
            {
                listBox1.Items.Add(d);
            }
        }
    }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex];
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(MediaPlayer_PlayStateChange);
    }

    public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        switch (axWindowsMediaPlayer1.playState)
        {
            // Locks up here
            case WMPLib.WMPPlayState.wmppsReady:
                axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
                break;
            case WMPLib.WMPPlayState.wmppsStopped:
                axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
                break;
            default:
                break;
        }
    }
}
}

EDIT:

I updated my code and this is what it looks like:

public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
 switch (axWindowsMediaPlayer1.playState)
 {
   case WMPLib.WMPPlayState.wmppsReady:                                     
    axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
    break;
    case WMPLib.WMPPlayState.wmppsStopped:
     axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
     break;
     default:
     break;
}
}

void change_song()
{
  axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
}

But when I run this, it gets stuck at "Media Changing" and eventually does nothing.

Jacob
  • 69
  • 14

0 Answers0