【问题标题】:BackgroundMediaPlayer set Uri source of Media library itemBackgroundMediaPlayer 设置媒体库项的 Uri 源
【发布时间】:2014-07-01 12:06:24
【问题描述】:

WP 8.1 有 BackgroundMediaPlayer 播放背景音频。

http://www.jayway.com/2014/04/24/windows-phone-8-1-for-developers-the-background-media-player/

在本教程中作者设置 ms-appx 源来播放歌曲:

BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///Assets/Media/Ring01.wma"));

成功。 但是,如何使用音乐库中的音频设置 UriSource?仅使用 SetUriSource 因为:

新的后台播放器提供了三种简单的歌曲播放方法。 SetFileSource、SetStreamSource 和 SetUriSource。只能从前台应用程序调用其中之一,即 SetUriSource 方法。其他需要从您的任务中使用。原因是后台播放器无法访问 SetFIleSource 或 SetStreamSource 所在的内存,除非在任务中设置。

在哪里可以阅读有关 WP8.1 的高级信息? 谢谢你的提前。

【问题讨论】:

    标签: c# audio windows-phone-8.1


    【解决方案1】:

    您不能使用 SetUriSource 访问库或可移动存储中的文件,至少不能从后台任务访问文件。您需要改用 SetFileSource 或 SetStreamSource。不幸的是,这在任何地方都没有记录。

    【讨论】:

      【解决方案2】:

      您可以使用文件的完整系统路径,使用 MusicLibrary 中的文件设置您的 Uri。比如这样:

      StorageFile file = (await KnownFolders.MusicLibrary.GetFilesAsync()).FirstOrDefault();
      BackgroundMediaPlayer.Current.SetUriSource(new Uri(file.Path, UriKind.RelativeOrAbsolute));
      

      【讨论】:

      • 不幸的是,它不起作用。 new Uri(pathSource, UriKind.RelativeOrAbsolute) 有这个字符串:{file:///C:/Data/Users/Public/Music/CD - 1/Test001.mp3}
      • 但是 new Uri("ms-appx:///Assets/Media/Ring01.wma") 工作正常。
      • @RegisteredUser 我已经测试过它工作得很好。你可以试试:new Uri(@"C:/Data/Users/Public/Music/CD - 1/Test001.mp3", UriKind.RelativeOrAbsolute)?您还添加了 MusicLibrary 的功能吗?
      • 不工作... 添加了音乐功能。顺便说一句,来自资产/媒体的音乐播放不完全。仅在 3-4 秒后停止。你能和我分享你的代码吗?
      • 我的后台任务代码是:public sealed class AudioPlayer : IBackgroundTask { private BackgroundTaskDeferral _deferral; public void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); taskInstance.Canceled += TaskInstanceOnCanceled; } private void TaskInstanceOnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { _deferral.Complete(); } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2016-09-02
      • 2013-11-30
      • 1970-01-01
      相关资源
      最近更新 更多