如何让媒体元素自动播放下一个列表框项目

本文关键字:下一个 列表 项目 自动播放 元素 媒体 | 更新日期: 2023-09-27 18:24:41

我正在制作一个媒体播放器,将媒体文件添加到列表框中。我想做的是让媒体元素在当前歌曲/视频结束后自动开始播放列表框中的下一首歌曲/视频。此外,我还想制作一个双击事件,如果列表项单击,则应播放歌曲/视频。这就是我目前拥有的:

Xaml:

<MediaElement Name="objMediaPlayer" LoadedBehavior="Manual" UnloadedBehavior="Stop" MediaOpened="objMediaPlayer_MediaOpened" MediaEnded="objMediaPlayer_MediaEnded" Margin="20,19,20,40" ButtonBase.Click="mediaItemList_MouseDoubleClick" />
<ListBox Canvas.Left="882" Canvas.Top="12" Height="467" Name="mediaItemList" Width="260" Background="Gray" MouseDoubleClick="mediaItemList_MouseDoubleClick" />

cs表:

 private void BrowseButtonClick(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
        dlg.Multiselect = true;
        dlg.InitialDirectory = "c:''";
        dlg.Filter = "All Files (*.*)|*.*";
        dlg.RestoreDirectory = true;
        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (string file in dlg.FileNames)
            {
                FileInfo fileName = new FileInfo(file);
                mediaItemList.Items.Add(fileName);
            }
            string selectedFileName = dlg.FileName;
            fileNameLabel.Content = selectedFileName;
            objMediaPlayer.Source = new Uri(selectedFileName);
            objMediaPlayer.Play();
            lblCoverUp.Visibility = Visibility.Hidden;

        }
    }
private int currentSongIndex = -1;
    private void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
    {
            if (currentSongIndex == -1)
            {
                currentSongIndex = mediaItemList.SelectedIndex;
            }
            currentSongIndex++;
            if (currentSongIndex < mediaItemList.Items.Count)
            {
                objMediaPlayer.Play();
            }
            else
            {
                // last song in listbox has been played
            }           
    }

    private void mediaItemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }

对于双击功能

    private void mediaItemList_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.Button prevButton = objMediaPlayer.Tag as System.Windows.Controls.Button;
        System.Windows.Controls.Button button = (sender as System.Windows.Controls.Button);
        FileInfo fileInfo = button.DataContext as FileInfo;
        // If a file is playing, stop it
        if (prevButton != null)
        {
            objMediaPlayer.Tag = null;
            objMediaPlayer.Stop();
            prevButton.Background = Brushes.LightYellow;
           // if the one thats playing is the one that was clicked -> don't play it
            if (prevButton == button)
                return;
        }
        // Play the one that was clicked
        objMediaPlayer.Tag = button;
        objMediaPlayer.Source = new Uri(fileInfo.FullName);
        objMediaPlayer.Play();
    }

对于Media_end,我已经尝试过:objMediaPlayer.Play(mediaItemList.Items[currentSongIndex]);

当激活双击事件时,我得到以下错误:对象引用未设置为对象的实例。(FileInfo FileInfo=button.DataContext为FileInfo)。

我希望有人能帮助我。如果你需要更多信息,只需询问

编辑:我已经通过使用以下代码修复了大部分媒体结束的问题:

    private void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
    {
        objMediaPlayer.Stop();
        if (mediaItemList.SelectedIndex <= mediaItemList.Items.Count)
        {
            mediaItemList.SelectedIndex = mediaItemList.SelectedIndex += 1;
            fileNameLabel.Content = mediaItemList.SelectedItem;
            objMediaPlayer.Play();
        }
        else
        {
            objMediaPlayer.Stop();
            fileNameLabel.Content = " ";
        }
    }

现在唯一的问题是播放器在完成最后一首歌曲(listitem)后没有停止。你是怎么解决的。

如何让媒体元素自动播放下一个列表框项目

在播放每个文件之前,您肯定需要打开它们吗?:

private void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
    if (currentSongIndex == -1)
    {
        currentSongIndex = mediaItemList.SelectedIndex;
    }
    currentSongIndex++;
    if (currentSongIndex < mediaItemList.Items.Count)
    {
        objMediaPlayer.Open(new Uri(mediaItemList.ElementAt(currentSongIndex), 
            UriKind.Absolute));            
    }
    else
    {
        // last song in listbox has been played
    }           
}
private void MediaPlayer_MediaOpened(object sender, EventArgs e)
{
    objMediaPlayer.Play();
}
相关文章: