如何在文件播放完后自动播放播放列表中的下一个文件?
How to automatically play the next file in playlist when a file have finished?
我用 C# WPF 做了自己的音乐播放器。但是,我无法让它在歌曲播放完毕后自动播放播放列表中的下一个文件。我有一个显示播放进度的滑块、一个复选框和下一个-上一个按钮。
这是“下一步”按钮的代码:
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
{
listBox.SelectedIndex = listBox.SelectedIndex + 1;
TagLib.File tagFile = TagLib.File.Create((listBox.SelectedValue).ToString());
string album = tagFile.Tag.Album;
string artist = tagFile.Tag.FirstAlbumArtist;
string title = tagFile.Tag.Title;
uint year = tagFile.Tag.Year;
string genre = tagFile.Tag.FirstGenre;
lblName.Content = artist + " - " + title;
lblAlbum.Content = album;
lblArtist.Content = artist;
lblTitle.Content = title;
lblYear.Content = year;
lblGenre.Content = genre;
lblBit.Content = tagFile.Properties.AudioBitrate + " kbps";
lblTime2.Content = tagFile.Properties.Duration.ToString(@"mm\:ss");
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
btnPlay2.Visibility = Visibility.Hidden;
btnPause.IsEnabled = true;
}
}
下面是滑块的代码:
private void sliProgress_DragStarted(object sender, DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sliProgress_DragCompleted(object sender, DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
mediaPlayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
}
private void sliProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
lblTime.Content = TimeSpan.FromSeconds(sliProgress.Value).ToString(@"mm\:ss");
}
我尝试通过比较 song/file (lblTime2) 的最大持续时间和进度持续时间 (lblTime) 是否相等来将“下一步”按钮中的过程执行到单击时的复选框中:
private void radioAll_Checked(object sender, RoutedEventArgs e)
{
if (lblTime.Content.ToString() == lblTime2.Content.ToString())
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
{
listBox.SelectedIndex = listBox.SelectedIndex + 1;
TagLib.File tagFile = TagLib.File.Create((listBox.SelectedValue).ToString());
string album = tagFile.Tag.Album;
string artist = tagFile.Tag.FirstAlbumArtist;
string title = tagFile.Tag.Title;
uint year = tagFile.Tag.Year;
string genre = tagFile.Tag.FirstGenre;
lblName.Content = artist + " - " + title;
lblAlbum.Content = album;
lblArtist.Content = artist;
lblTitle.Content = title;
lblYear.Content = year;
lblGenre.Content = genre;
lblBit.Content = tagFile.Properties.AudioBitrate + " kbps";
lblTime2.Content = tagFile.Properties.Duration.ToString(@"mm\:ss");
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
}
}
}
不幸的是,它根本无法工作。我错过了什么吗?我该怎么办?
订阅MediaPlayer.MediaEnded
事件并适当处理。
MSDN:
Occurs when the media has finished playback
例如
mediaPlayer.MediaEnded += OnMediaEnded;
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
.
.
.
private void OnMediaEnded(object sender, EventArgs e)
{
// play next song
}
我用 C# WPF 做了自己的音乐播放器。但是,我无法让它在歌曲播放完毕后自动播放播放列表中的下一个文件。我有一个显示播放进度的滑块、一个复选框和下一个-上一个按钮。
这是“下一步”按钮的代码:
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
{
listBox.SelectedIndex = listBox.SelectedIndex + 1;
TagLib.File tagFile = TagLib.File.Create((listBox.SelectedValue).ToString());
string album = tagFile.Tag.Album;
string artist = tagFile.Tag.FirstAlbumArtist;
string title = tagFile.Tag.Title;
uint year = tagFile.Tag.Year;
string genre = tagFile.Tag.FirstGenre;
lblName.Content = artist + " - " + title;
lblAlbum.Content = album;
lblArtist.Content = artist;
lblTitle.Content = title;
lblYear.Content = year;
lblGenre.Content = genre;
lblBit.Content = tagFile.Properties.AudioBitrate + " kbps";
lblTime2.Content = tagFile.Properties.Duration.ToString(@"mm\:ss");
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
btnPlay2.Visibility = Visibility.Hidden;
btnPause.IsEnabled = true;
}
}
下面是滑块的代码:
private void sliProgress_DragStarted(object sender, DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sliProgress_DragCompleted(object sender, DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
mediaPlayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
}
private void sliProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
lblTime.Content = TimeSpan.FromSeconds(sliProgress.Value).ToString(@"mm\:ss");
}
我尝试通过比较 song/file (lblTime2) 的最大持续时间和进度持续时间 (lblTime) 是否相等来将“下一步”按钮中的过程执行到单击时的复选框中:
private void radioAll_Checked(object sender, RoutedEventArgs e)
{
if (lblTime.Content.ToString() == lblTime2.Content.ToString())
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
{
listBox.SelectedIndex = listBox.SelectedIndex + 1;
TagLib.File tagFile = TagLib.File.Create((listBox.SelectedValue).ToString());
string album = tagFile.Tag.Album;
string artist = tagFile.Tag.FirstAlbumArtist;
string title = tagFile.Tag.Title;
uint year = tagFile.Tag.Year;
string genre = tagFile.Tag.FirstGenre;
lblName.Content = artist + " - " + title;
lblAlbum.Content = album;
lblArtist.Content = artist;
lblTitle.Content = title;
lblYear.Content = year;
lblGenre.Content = genre;
lblBit.Content = tagFile.Properties.AudioBitrate + " kbps";
lblTime2.Content = tagFile.Properties.Duration.ToString(@"mm\:ss");
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
}
}
}
不幸的是,它根本无法工作。我错过了什么吗?我该怎么办?
订阅MediaPlayer.MediaEnded
事件并适当处理。
MSDN:
Occurs when the media has finished playback
例如
mediaPlayer.MediaEnded += OnMediaEnded;
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
.
.
.
private void OnMediaEnded(object sender, EventArgs e)
{
// play next song
}