Это код, который я сейчас использую:
ПРИМЕЧАНИЕ: переменная в очереди[currentQueue].getTracks()[trackIndex] является StorageFile извлекается напрямую из средства выбора файлов — никакие преобразования в другие типы и т. д. не производятся.
Код: Выделить всё
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();
}
Интересная особенность заключается в том, что этот код работает по назначению и успешно воспроизводит файлы (и переменная существует имеет значение true, что означает, что путь к файлу правильный, но только при компиляции и запуске в Windows, при запуске на Android он не работает; .
Как исправить эту ошибку и заставить MediaPlayerElement воспроизводить правильный файл как на Android, так и на Windows?
Подробнее здесь: https://stackoverflow.com/questions/786 ... -device-in