【问题标题】:How can you let the media-element automatically play the next list-box item?如何让媒体元素自动播放下一个列表框项目?
【发布时间】:2014-11-20 05:22:42
【问题描述】:

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

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-sheet:

 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_ended 我已经尝试过了:objMediaPlayer.Play(mediaItemList.Items[currentSongIndex]);

激活 Doubleclick 事件时,我收到以下错误:对象引用未设置为对象的实例。 (FileInfo fileInfo = button.DataContext as 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 = " ";
        }
    }

现在唯一的问题是播放器在播放完最后一首歌曲(列表项)后不会停止。你如何解决这个问题。

【问题讨论】:

  • 那是因为您不是双击一个按钮而是双击列表框。在该行下一个断点,并在调试器中查看 sender 的值。
  • 这是我调试后得到的,这对我来说毫无意义。 Nova.exe 中发生了“System.NullReferenceException”类型的第一次机会异常 步入:跨过非用户代码“MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen” 线程“”(0x1638)已退出代码 0 (0x0)。
  • 对不起,我的陈述是围绕你的第二个问题。行 System.Windows.Controls.Button button = (sender as System.Windows.Controls.Button);正在设置 button = null 因为发件人不是按钮。然后下一行因此抛出 NullReferenceException。
  • 好的,这是有道理的。你知道我应该让发件人做什么吗?可能是 selectedindex 还是列表项?

标签: c# wpf listboxitem autoplay


【解决方案1】:

您确实需要在播放之前打开每个文件吗?:

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();
}

【讨论】:

  • objMediaplayer 没有 Open 的定义。你能不能解释一下你做了什么。谢谢!
  • 哦,抱歉,我没有意识到您命名不佳的objMediaPlayer 对象实际上是MediaElement。尝试使用实际的 MediaPlayer 对象。
  • 我正在使用 wpf,每次尝试将媒体播放器添加到我的工具箱中时(当然,在添加参考之后)它都会失败。我添加了控件,但它没有出现在我的工具箱中。这就是我改用媒体元素的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多