How to play all files in a folder. This feature does not work correctly:
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
fileList = await folder.GetFilesAsync();
foreach (StorageFile file in fileList)
{
mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
mediaPlayerElement.MediaPlayer.Play(); // <-?? (only the last file in folder)
}
}
I found a solution.
int ind;
private StorageFolder folder = null;
private IReadOnlyList<StorageFile> fileList = null;
public MainPage()
{
InitializeComponent();
mediaPlayerElement.MediaPlayer.MediaEnded += MediaPlayer_VideoFrameEndedAsync;
}
private async void MediaPlayer_VideoFrameEndedAsync(object sender, object args)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
ind++;
if (ind < fileList.Count)
{
mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(fileList[ind]);
mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
mediaPlayerElement.MediaPlayer.Play();
notify.Text = fileList[ind].DisplayName;
}
});
}
private async void folder_ClickAsync(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
fileList = await folder.GetFilesAsync();
if (fileList.Count > 0)
{
ind = 0;
mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(fileList[ind]);
mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
mediaPlayerElement.MediaPlayer.Play(); // <-?? (only the last file in folder)
notify.Text = fileList[ind].DisplayName;
}
//foreach (StorageFile file in fileList)
//{
// mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
// mediaPlayerElement.MediaPlayer.PlaybackSession.PlaybackRate = 2;
// mediaPlayerElement.MediaPlayer.Play(); // <-?? (only the last file in folder)
// notify.Text = file.DisplayName;
//}
}
else
{
notify.Text = "Operation cancelled.";
}
}