C# Mp3播放课程播放列表中的歌曲,但要等到其中一首完成后才能继续
我目前正在使用c#和winforms播放mp3文件,但我添加了一个datagridview来列出歌曲,现在当我在网格中单击一首歌曲时,它可以播放歌曲,但它只播放那一首歌曲,我想做的是,一旦歌曲播放完毕,就转到列表中的下一首歌曲。我已经尝试过线程。以AudioLength作为睡眠时间,但这只会停止整个应用程序的工作,直到它完成睡眠这根本不是我想要的,我对winforms有点陌生,所以如果有人能告诉我需要做哪些更改才能使其工作,我将非常感激。这是我目前掌握的代码:C# Mp3播放课程播放列表中的歌曲,但要等到其中一首完成后才能继续,c#,winforms,multithreading,datagridview,thread-sleep,C#,Winforms,Multithreading,Datagridview,Thread Sleep,我目前正在使用c#和winforms播放mp3文件,但我添加了一个datagridview来列出歌曲,现在当我在网格中单击一首歌曲时,它可以播放歌曲,但它只播放那一首歌曲,我想做的是,一旦歌曲播放完毕,就转到列表中的下一首歌曲。我已经尝试过线程。以AudioLength作为睡眠时间,但这只会停止整个应用程序的工作,直到它完成睡眠这根本不是我想要的,我对winforms有点陌生,所以如果有人能告诉我需要做哪些更改才能使其工作,我将非常感激。这是我目前掌握的代码: private void dgvT
private void dgvTracks_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
PlayFiles(e.RowIndex);
}
public void PlayFiles(int index)
{
try
{
int eof = dgvTracks.Rows.Count;
for (int i = index; index <= eof; i++)
{
if (File.Exists(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString()))
{
PlayFile(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString());
Application.DoEvents();
Thread.Sleep(TimeSpan.FromSeconds(mplayer.AudioLength));
}
else
{
Exception a = new Exception("File doesn't exists");
throw a;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Message, MessageBoxButtons.OK);
}
}
public void PlayFile(string filename)
{
mplayer.Open(filename);
mplayer.Play();
}
private void dgvTracks\u cell双击(对象发送方,DataGridViewCellEventArgs e)
{
播放文件(e.RowIndex);
}
公共void播放文件(int索引)
{
尝试
{
int eof=dgvTracks.Rows.Count;
对于(int i=index;index停止
和关闭
播放新音频之前的上一次播放。例如,如果您使用
我假设mplayer
是System.Windows.Media.MediaPlayer
的一个实例
您可以订阅MediaPlayer.MediaEnded
事件,并使用事件处理程序开始播放下一个文件。您需要将当前播放的索引存储在某个位置
...
int currentPlayIndex = -1;
...
mplayer.MediaEnded += OnMediaEnded;
...
private void OnMediaEnded(object sender, EventArgs args)
{
// if we want to continue playing...
PlayNextFile();
}
...
public void PlayNextFile()
{
PlayFiles(currentPlayIndex + 1);
}
public void PlayFiles(int index)
{
try
{
currentPlayIndex = -1;
int eof = dgvTracks.Rows.Count;
for (int i = index; index <= eof; i++)
{
if (File.Exists(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString()))
{
currentPlayIndex = i; // <--- save index
PlayFile(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString());
Application.DoEvents();
Thread.Sleep(TimeSpan.FromSeconds(mplayer.AudioLength));
}
else
{
Exception a = new Exception("File doesn't exists");
throw a;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Message, MessageBoxButtons.OK);
}
}
。。。
int currentPlayIndex=-1;
...
mplayer.mediated+=onmediated;
...
MediaEnded上的私有void(对象发送方、事件args args)
{
//如果我们想继续比赛。。。
PlayNextFile();
}
...
public void PlayNextFile()
{
播放文件(当前播放索引+1);
}
公共void播放文件(int索引)
{
尝试
{
currentPlayIndex=-1;
int eof=dgvTracks.Rows.Count;
for(int i=索引;索引
...
int currentPlayIndex = -1;
...
mplayer.MediaEnded += OnMediaEnded;
...
private void OnMediaEnded(object sender, EventArgs args)
{
// if we want to continue playing...
PlayNextFile();
}
...
public void PlayNextFile()
{
PlayFiles(currentPlayIndex + 1);
}
public void PlayFiles(int index)
{
try
{
currentPlayIndex = -1;
int eof = dgvTracks.Rows.Count;
for (int i = index; index <= eof; i++)
{
if (File.Exists(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString()))
{
currentPlayIndex = i; // <--- save index
PlayFile(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString());
Application.DoEvents();
Thread.Sleep(TimeSpan.FromSeconds(mplayer.AudioLength));
}
else
{
Exception a = new Exception("File doesn't exists");
throw a;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Message, MessageBoxButtons.OK);
}
}