简体   繁体   English

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

[英]How can you let the media-element automatically play the next list-box item?

I'm making a media player where I add media files to a list-box. 我正在制作一个媒体播放器,在其中将媒体文件添加到列表框中。 What I would like to do is to make the media-element automatically start playing the next song/video in the list-box after the current one has ended. 我想做的是使媒体元素在当前播放结束后自动开始播放列表框中的下一首歌曲/视频。 Also I would like to make a doubleclickevent where if listitem clicked the song/video should play. 我也想做一个doubleclick事件,如果单击listitem,则应该播放歌曲/视频。 This is what I currently have: 这是我目前拥有的:

Xaml: 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: 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)
    {

    }

For the Doubleclick function 对于Doubleclick功能

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

For Media_ended I have already tried: objMediaPlayer.Play(mediaItemList.Items[currentSongIndex]); 对于Media_ended,我已经尝试过:objMediaPlayer.Play(mediaItemList.Items [currentSongIndex]);

When activating the Doubleclick event I get the following error:Object reference not set to an instance of an object. 激活Doubleclick事件时,出现以下错误:对象引用未设置为对象的实例。 (FileInfo fileInfo = button.DataContext as FileInfo). (FileInfo fileInfo = button.DataContext作为FileInfo)。

I hope someone can help me. 我希望有一个人可以帮助我。 If you need more information, just ask 如果您需要更多信息,只需询问

EDIT: I have fixed mostly of the media ended problem by using the following code: 编辑:我已经通过使用以下代码修复了大多数媒体结束问题:

    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 = " ";
        }
    }

The only problem now is that the player doesn't stop after finishing the last song(listitem). 现在唯一的问题是,播放完最后一首歌曲(listitem)后播放器不会停止播放。 How do you fix this. 您如何解决此问题。

Surely you need to open each file before you play them?: 确定要播放每个文件吗?:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM