音频播放列表 uwp mediaelement c#

audio playslist uwp mediaelement c#

存储文件 ObservableCollection<StorageFile> files;

public MainPage()
{
    this.InitializeComponent();
    files = new ObservableCollection<StorageFile>();
    _fileList.ItemsSource = files;
    this.DataContext = this;
} 

打开文件位置。文件位置打开,音频添加到列表

async private System.Threading.Tasks.Task AddMedia()
{
    //Create a new picker
    var filePicker = new Windows.Storage.Pickers.FileOpenPicker();

    //Add filetype filters. 

    filePicker.FileTypeFilter.Add(".mp3");
    filePicker.FileTypeFilter.Add(".wav");


    //Set picker start location to the video library
    filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;

    //Retrieve single file from picker
    StorageFile file;
    file = await filePicker.PickSingleFileAsync();

    if (file != null)
    {
        files.Add(file);
        mediaElement.Play();
    }

}

从列表视图中选择文件 该文件播放但一旦结束就不会播放列表中的下一个文件。我想自动播放列表中的下一个,但我正在努力播放,请帮助

private async void _fileList_ItemClick(object sender, ItemClickEventArgs e)
{   
    var storageitem = e.ClickedItem as StorageFile;
    IRandomAccessStream stream = await 
    storageitem.OpenAsync(FileAccessMode.Read);
    mediaElement.SetSource(stream, storageitem.ContentType);
    this.txt_song.Text = "Now playing: " + storageitem.Name;
}

只需使用两个 MediaElements 即可实现您的要求。

One solution is to use two MediaElements. One MediaElemet is always playing and the second is opening and loading the next track in the background ready to start playback as soon as it is needed. When one playlist item ends we can immediately start playback of the next. Then the one that was just playing starts to queue the next item. Sounds simple right?

The key to making any playlist solution work with the MediaElement is to handle the “MediaEnded” event. When we receive this event we know that the item is done playing. We can then check the playlist and start the next item playing. Luckily unlike with the Media Player SDK, the MediaElement guarantees that we will always get the “MediaEnded” event. Without this a playlist solution would be impossible.

https://blogs.msdn.microsoft.com/wsdevsol/2014/07/11/multi-mediaelement-a-playlist-solution-next-item-no-waiting/

你应该使用 MediaPlaybackList for creating music playlists and easily managing them, it helps you to move to next media file or the previous one very easily, supports gap-less playback, and in the long term it will make your life very easier to control a playlist fully. It is best if you use it with the ui control MediaPlayerElement