【问题标题】:How to automatically play the next file in playlist when a file have finished?文件完成后如何自动播放播放列表中的下一个文件?
【发布时间】:2017-09-01 02:01:47
【问题描述】:

我在 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");
}

通过比较歌曲/文件的最大持续时间 (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();
            }
        }
    }

不幸的是,它根本无法工作。我错过了什么吗?我该怎么办?

【问题讨论】:

    标签: c# wpf playlist


    【解决方案1】:

    订阅MediaPlayer.MediaEnded 事件并适当处理。

    MSDN:

    在媒体播放完毕时发生

    例如

    mediaPlayer.MediaEnded += OnMediaEnded;
    
    mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
    mediaPlayer.Play();
    .
    .
    .
    
    private void OnMediaEnded(object sender, EventArgs e) 
    {
        // play next song
    }    
    

    【讨论】:

    • 感谢您的帮助,它帮助了我!
    • 我读了这个thread 来帮助我更多。现在我也可以连续重复一个文件了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多