简体   繁体   中英

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. This is what I currently have:

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

    }

For the Doubleclick function

    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]);

When activating the Doubleclick event I get the following error:Object reference not set to an instance of an object. (FileInfo fileInfo = button.DataContext as 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). 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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