本帖最后由 袁晓俊 于 2014-5-16 11:12 编辑
出现下面的问题,求解决,,,,,不能自动播放下一首,,,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 音乐播放器
{
public partial class Form1 : Form
{
List<string> lSongs = new List<string>();//用于存放歌曲的数据库(核心)
public Form1()
{
InitializeComponent();
}
private void AddURL_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
axPlayer.URL = ofd.FileName;
btnPlay.Enabled = true;
btnStop.Enabled = true;
btnMute.Enabled = true;
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
// axPlayer.Ctlcontrols.play();
if (btnPlay.Text == "播放")
{
axPlayer.Ctlcontrols.play();
btnPlay.Text = "暂停";
}
else if (btnPlay.Text == "暂停")
{
axPlayer.Ctlcontrols.pause();
btnPlay.Text = "播放";
}
}
//停止
private void btnStop_Click(object sender, EventArgs e)
{
axPlayer.Ctlcontrols.stop();
btnPlay.Text = "播放";
}
//静音、取消静音
private void btnMute_Click(object sender, EventArgs e)
{
if (btnMute.Text == "静音")
{
axPlayer.settings.mute = true;
btnMute.Text = "取消静音";
}
else if (btnMute.Text == "取消静音")
{
axPlayer.settings.mute = false;
btnMute.Text = "静音";
}
}
//打开部分文件
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"E:\KuGou";//定义默认的文件路径
ofd.Filter = "音乐文件|*.mp3;*.wma";//定义只查找的文件类型
ofd.Title = "请选择文件";//对话框的窗体标示
ofd.Multiselect = true;//文件可以多个选择
if (ofd.ShowDialog() == DialogResult.OK)//如果打开成功
{
btnPrav.Enabled = true;
btnPlay.Enabled = true;
btnMute.Enabled = true;
btnStop.Enabled = true;
//listSongsPlayer.Items.AddRange(ofd.FileNames);
//axPlayer.URL = listSongsPlayer.Items[1].ToString();
for (int i = 0; i < ofd.FileNames.Length; i++)//遍历文件的文件名
{
lSongs.Add(ofd.FileNames);
string[] temp = ofd.FileNames.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
listSongsPlayer.Items.Add(temp[temp.Length - 1]);
}
MyPlay();//默认播放第0shou
}
}
//播放索引
private void MyPlay(int i)
{
axPlayer.URL = lSongs;
axPlayer.Ctlcontrols.play();
listSongsPlayer.SelectedIndex = i;
btnPlay.Text = "暂停";
}
//播放方法,后台列表选择和播放前台选项高亮显示
private void MyPlay()
{
MyPlay(0);
}
private void btnNext_Click(object sender, EventArgs e)
{
if(listSongsPlayer.Items.Count>0)
MyPlay(listSongsPlayer.SelectedIndex + 1 > lSongs.Count - 1 ? 0 : ++listSongsPlayer.SelectedIndex);
}
private void btnPrav_Click(object sender, EventArgs e)
{
if(listSongsPlayer.Items.Count>0)
MyPlay(listSongsPlayer.SelectedIndex - 1 < 0 ? lSongs.Count - 1 : --listSongsPlayer.SelectedIndex);
}
//双击播放
private void listSongsPlayer_DoubleClick(object sender, EventArgs e)
{
MyPlay(listSongsPlayer.SelectedIndex);
}
//右击删除
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listSongsPlayer.SelectedIndex !=-1)
{
lSongs.RemoveAt(listSongsPlayer.SelectedIndex);
listSongsPlayer.Items.RemoveAt(listSongsPlayer.SelectedIndex);
}
}
//清空播放列表
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
lSongs.Clear();
listSongsPlayer.Items.Clear();
}
private void axPlayer_PlayStateChange_1(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
int temp = 0;
if(lSongs.Count==0)
{
return;
}
if (axPlayer.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
temp = listSongsPlayer.SelectedIndex;
temp++;
if (temp > lSongs.Count - 1)
{
listSongsPlayer.SelectedIndex = 0;
axPlayer.URL = lSongs[0];
}
else
{
listSongsPlayer.SelectedIndex = temp;
axPlayer.URL = lSongs[temp];
}
}
if (axPlayer.playState == WMPLib.WMPPlayState.wmppsReady)
{
axPlayer.Ctlcontrols.play();
}
}
}
}
|
|