How to stop music playing after last song in `Listview`

Viewed 101

In my UWP app for Raspberry pi3 I have a music player that plays songs from a ListView. You can select any song in the ListView and then it will automatically play the next song. The problem I am facing is that when it reaches the last song and finishes it plays it again and doesn't stop. Like it goes in a loop on the last song instead of stopping after playing it.

Can someone direct me on how to stop the loop?

This is my code:

    private async void SongClicked(object sender, ItemClickEventArgs e)
    {
        var file = await KnownFolders.MusicLibrary.GetFileAsync(e.ClickedItem.ToString());
        if (file != null)
        {
            var stream = await file.OpenReadAsync();
            mediaElement.SetSource(stream, file.ContentType);
            mediaElement.Play();
        }
    }

    private void mediaItemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void objMediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
    {
        mediaElement.Play();
    }

    private async void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
    {
        if (AudioFilesLV.SelectedIndex <= AudioFilesLV.Items.Count)
        {
            AudioFilesLV.SelectedIndex = AudioFilesLV.SelectedIndex = 1;
            var file = await KnownFolders.MusicLibrary.GetFileAsync(AudioFilesLV.SelectedItem.ToString());
            if (file != null)
            {
                var stream = await file.OpenReadAsync();
                mediaElement.SetSource(stream, file.ContentType);
                mediaElement.Play();
            }
        }
        else
        {
            mediaElement.Stop();
        }
    }

Any help will be appreciated.

Thanks.

2 Answers

Hi and welcome to stackoverflow, could it possible that the problem is in the first line of the if condition from the event objMediaPlayer_MediaEnded. Because you reach the end of the list and set

AudioFilesLV.SelectedIndex = AudioFilesLV.SelectedIndex = 1;

The SelectedIndex should now again smaller then AudioFilesLV.Items.Count.

The line should change to:

AudioFilesLV.SelectedIndex = AudioFilesLV.SelectedIndex + 1;

Bcause then the you count up the the SelectedIndex in the if case. At the moment the SelectedIndex should always be 1.

Her are an example if you need to count up the SelectedIndex to select the next song:

private async void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
    // If the end of the ListView is reached and the last song was played stop.
    if ((AudioFilesLV.SelectedIndex + 1) == AudioFilesLV.Items.Count)
    {
       mediaElement.Stop();
    }
    else
    {
       // This line you should try to change. When the last song was not played 
       //-> select next one and play them.
        AudioFilesLV.SelectedIndex = AudioFilesLV.SelectedIndex + 1;
        var file = await KnownFolders.MusicLibrary.GetFileAsync(AudioFilesLV.SelectedItem.ToString());
        if (file != null)
        {
            var stream = await file.OpenReadAsync();
            mediaElement.SetSource(stream, file.ContentType);
            mediaElement.Play();
        }
    }
}

Then for you information why I have done:

(AudioFilesLV.SelectedIndex + 1)

in the example. Because AudioFilesLV.SelectedIndex (SelectedIndex start at 0) is one less then AudioFilesLV.Items.Count (Count starts at 1) in every case.

I hope this helps you.

Try this,

private async void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
    {
        AudioFilesLV.SelectedIndex += 1;
        if (AudioFilesLV.SelectedIndex <= AudioFilesLV.Items.Count - 1)
        {
            var file = await KnownFolders.MusicLibrary.GetFileAsync(AudioFilesLV.SelectedItem.ToString());
            if (file != null)
            {
                var stream = await file.OpenReadAsync();
                mediaElement.SetSource(stream, file.ContentType);
                mediaElement.Play();
            }
        }
        else
        {
            mediaElement.Stop();
        }
    }
Related