BackgroundMediaPlayer不播放音乐文件夹外的音乐文件

时间:2015-12-01 07:11:55

标签: c# audio windows-phone-8.1 background-process

我遇到这个奇怪的问题。我正在尝试为Windows Phone 8.1创建一个音乐播放器应用。我需要音乐播放器在后台播放音乐,即使应用程序处于后台/关闭状态。所以我使用BackgroundMediaPlayer课来播放音乐。以下是相同的代码:

private void SendMessageToBackground(string Path, string Name, bool IsRadio)
{
    if (!string.IsNullOrEmpty(Path))
    {
        var message = new ValueSet();
        message.Add(Constants.TrackURI, Path);
        message.Add(Constants.TrackName, Name);
        message.Add(Constants.IsRadio, IsRadio);
        BackgroundMediaPlayer.MessageReceivedFromBackground += BackgroundMediaPlayer_MessageReceivedFromBackground;
        BackgroundMediaPlayer.Current.MediaFailed += Current_MediaFailed;
        BackgroundMediaPlayer.SendMessageToBackground(message);
    }
}

代码工作正常,音乐播放流畅。但现在我面临一个问题。我通过循环遍历所有文件夹来获取应用程序中的音乐文件。因此,它显示所有音乐文件,无论它们位于文件夹层次结构中。但是,当我尝试在Windows手机创建的Music文件夹之外播放文件时(请参阅屏幕截图),背景音乐播放器根本无法播放该文件。

Music File Issue

如代码中所示,我正在监听来自BackgroundMediaPlayer及其MediaPlayer的事件,以检查是否存在任何可能的故障原因,但在播放失败的情况下,这些事件都不会被触发。但是,如果成功播放(来自“音乐”文件夹),BackgroundMediaPlayer的{​​{1}}事件将被正确触发。我检查了传递的文件路径,这一切都是正确的,我尝试使用手机的内置音乐应用程序播放这些音乐文件,这也成功播放。这可能是什么问题?我该如何解决?

修改

我进一步调试并发现它实际上是一个权限问题。从我在另一个函数中使用的上述代码传递的路径,将MessageReceivedFromBackground / Uri传递给StorageFile。代码:

BackgroundMediaPlayer.Current

我已在应用的清单文件中设置了所需的功能。但是当我尝试运行此应用程序时,我得到了以下public async void StartTrackAt(string TrackURI, string Name) { try { BackgroundMediaPlayer.Current.SetUriSource(new Uri(TrackURI)); // This works fine in debug mode. Throws Exception in release mode /* Tried this also, but this doesn't even work in debug mode StorageFile storageFile = await StorageFile.GetFileFromPathAsync(TrackURI); mediaPlayer.SetFileSource(s); */ } catch (Exception ex) { //Control comes here when trying to play files outside Music folder. } }

Exception

奇怪的是,直接传递{System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at BackgroundMusicPlayer.MyPlayListManager.<StartTrackAt>d__0.MoveNext()} [System.UnauthorizedAccessException]: {System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at BackgroundMusicPlayer.MyPlayListManager.<StartTrackAt>d__0.MoveNext()} Data: {System.Collections.ListDictionaryInternal} HelpLink: null HResult: -2147024891 InnerException: null Message: "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" Source: "mscorlib" StackTrace: " at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at BackgroundMusicPlayer.MyPlayListManager.<StartTrackAt>d__0.MoveNext()" 而不提取Uri时,它在调试模式下运行良好。

以下是我为应用程序设置的功能:

App Capabilities

1 个答案:

答案 0 :(得分:0)

我遇到了与BackgroundMediaPlayer相同的访问问题。

see this answer about possible access to different files。 所以你需要组合FolderPicker和FutureAccessList

var folder = await folderPicker.PickSingleFolderAsync();
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder);