Reputation: 75
I am trying, in the Uno Platform using C#, to set the source of my MediaPlayerElement to a file picked by a FileOpenPicker. I have successfully retrieved the resulting file path of the picked file as a StorageFile object, but now I need to tell my MediaPlayerElement
to play the file at that location.
This is the code I am currently using:
NOTE: the variable at
queues[currentQueue].getTracks()[trackIndex]
is aStorageFile
directly retrieved from the file picker -- no conversions are made to other types, etc.
private async void PlayTrack(int queueIndex, int trackIndex)
{
MediaPlayer.MediaPlayer.Pause();
FileInfo fileInfo = new FileInfo(queues[currentQueue].getTracks()[trackIndex].Path);
bool exists = fileInfo.Exists;
// Show debugging dialog.
ContentDialog infoDialog = new ContentDialog();
infoDialog.Title = "Test";
infoDialog.CloseButtonText = "Ok";
infoDialog.DefaultButton = ContentDialogButton.None;
infoDialog.Content = "The file at '" + queues[currentQueue].getTracks()[trackIndex].Path + "' exists: " + exists;
infoDialog.XamlRoot = this.XamlRoot;
await infoDialog.ShowAsync();
// Create a MediaSource from the StorageFile.
string filePath = queues[queueIndex].getTracks()[trackIndex].Path;
filePath = filePath.TrimStart("content://".ToCharArray());
Uri fileUri = new Uri($"file://{filePath}");
MediaSource mediaSource = MediaSource.CreateFromUri(fileUri);
// Set the MediaSource to the MediaPlayer.
MediaPlayer.Source = mediaSource;
// Play the audio.
MediaPlayer.MediaPlayer.Play();
}
However, when run, the testing dialog shows that my exists
variable is false, meaning the believed file path is incorrect for some reason, and when I try to set it as my MediaPlayerElement
's source, it just prints in the debugging console "[MediaPlayer] mediaplayer went away with unhandled events"
and no error message is displayed nor does the app crash. After this, the player is completely unresponsive, (play/pause button doesn't do anything, etc).
An interesting quirk is that this code works as intended and plays files successfully (and the variable exists
states true, meaning that the file path is correct, but only when compiled and run on Windows; when run on Android, it does not work.
How do I fix this error and get my MediaPlayerElement
to play the correct file on both Android and Windows?
Upvotes: 0
Views: 58