0
votes

I am struggling to play the next song automatically after the last song has played in my ListView

I have a ListView that displays all the songs with Album, AlbumCover and Title and when clicking on a song it will play that song but when moving to the next song it plays the first song again. Even If i click on the next song in the list it plays the first song again?

I am really stumped with this one.

Here is my code:

private async Task InitFolderAsync()
    {
        StorageFolder musicLib = KnownFolders.MusicLibrary;
        var files = await musicLib.GetFilesAsync();
        foreach (var file in files)
        {
            StorageItemThumbnail currentThumb = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 50, ThumbnailOptions.UseCurrentScale);
            var albumCover = new BitmapImage();
            albumCover.SetSource(currentThumb);

            var musicProperties = await file.Properties.GetMusicPropertiesAsync();
            var musicname = musicProperties.Title;
            var musicdur = musicProperties.Duration;

            var artist = musicProperties.Artist;
            if (artist == "")
            {
                artist = "Unknown";
            }

            var album = musicProperties.Album;
            if (album == "")
            {
                album = "Unkown";
            }
            MusicList.Add(new MusicLib
            {
                FileName = musicname,
                Artist = artist,
                Album = album,
                Duration = musicdur,
                AlbumCover = albumCover,
                MusicPath = file.Path
            });

        }
    }


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

    private void objMediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
    {
        mediaElement.Play();
    }
1
So neither SongClicked nor objMediaPlayer_MediaEnded works as expected? Please share your XAML markup.mm8

1 Answers

0
votes

It's hard to say why it won't play with the code you provided. To me, it looks like it should work. Maybe for some reason, you are getting the same file in the GetFileAsync method instead of the new one. Anyway, Instead of handling the playlist yourself and using the obsolete MediaElement, I would advise you to use the MediaPlayerElement and a built-in component MediaPlaybackList or even MediaPlayer itself with MediaPlaybackList. You can read about MediaPlaybackList here: https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/media-playback-with-mediasource.