本文介绍了如何在 Application 上从 MediaElement 切换到 BackgroundMediaPlayer 失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像 PlayMusic.xaml 这样的音乐播放器页面,它使用 MediaPlayer 元素来播放音乐.

I have a music player page like PlayMusic.xaml and it uses MediaPlayer element to play music.

我想在用户单击后退按钮或 Windows 按钮或锁定屏幕时切换 BackgroundMediaPlayer.

I want to switch BackgroundMediaPlayer whenever user clicks back button or Windows Button or Lock Screen.

我也想继续留下歌曲的地方.(比如 BackgroundMediaPlayer 应该从第 30 秒开始)

I also would like to continue where ever song is remained. (Like BackgroundMediaPlayer should continue from 30th second)

Windows 手机中是否有任何机制,例如当我从 MediaPlayer 失去焦点时,我可以切换到 BackgroundMediaPlayer 并继续播放音乐?

Is there any mechanism in Windows phone like when ever when I lose focus from MediaPlayer I can switch to BackgroundMediaPlayer and continue playing music?

注意:我可以直接使用 BackgroundMediaPlayer 然后它总是播放音乐,但在这种情况下,我不能在 PlayMusic.xaml 中使用 MediaPlayer 这可以全屏观看视频剪辑并寻求控制.

Note: I can directly use BackgroundMediaPlayer then it always plays music but on this case I cannot use MediaPlayer in PlayMusic.xaml which gives ability to watch video clip, fullscreen and seek controls.

推荐答案

据我所知,没有具体的机制可以解决这个问题.您可以自己构建一个,但并不简单:

There is no specific mechanism for this that I know of. You can build one yourself, but it is not simple:

  1. 首先,您需要有一个有效的后台音频任务并知道如何与之通信(这本身并不容易,这个例子对我有帮助.
  2. 当您开始播放 MediaPlayer 中的任何内容时,您的后台音频任务将被取消.当你再次使用它时,它的 Run 方法会在同一个进程中再次被调用.因此,后台任务必须对其生命周期进行适当管理,即您需要订阅事件并在 Run 方法中获得延迟,并取消订阅事件,调用 BackgroundMediaPlayer.Shutdown() 并在 Cancel 事件中释放延迟.另外,我发现重新启动任务后需要在前台进程中再次订阅BackgroundMediaPlayer.MessageReceivedFromBackground,因为重新启动似乎清除了它.
  3. 您可以在导航页面时检测到 MediaPlayer 失去焦点"(并开始后台播放).但是,这不会涵盖用户远离您的应用程序进行多任务处理的情况或屏幕被关闭的情况.您可以使用 CoreWindow.GetForCurrentThread().Activated 事件捕获这些.
  4. 当您切换到背景音频时,您需要将当前位置传递给它.这可以使用消息传递 (BackgroundMediaPlayer.SendMessageToBackground) 来完成.
  5. 您可以通过关闭 AutoPlay 并在 MediaOpened 触发后设置位置来在指定位置开始后台播放.
  1. First, you need to have a working background audio task and know how to communicate with it (this is not easy in itself, this example helped me a bit).
  2. When you start playing anything in the MediaPlayer, your background audio task gets canceled. When you use it again, its Run method is called once more in the same process. Because of this, the background task must have its lifecycle properly managed, i.e. you need to subscribe to events and get a deferral in the Run method, and unsubscribe from events, call BackgroundMediaPlayer.Shutdown() and release the deferral in the Cancel event. Also, I found that I need to subscribe to BackgroundMediaPlayer.MessageReceivedFromBackground in the foreground process again after the task is restarted, because the restart seems to clear it.
  3. You can detect that the MediaPlayer "lost focus" (and start background playback) when the page is being navigated from. However, this will not cover the case of the user multitasking away from your application or the case of the screen being turned off. You can catch these using the CoreWindow.GetForCurrentThread().Activated event.
  4. When you are switching to background audio, you need to pass it the current position. This can be done using messaging (BackgroundMediaPlayer.SendMessageToBackground).
  5. You can start the background playback on the specified position by turning AutoPlay off and setting the position after MediaOpened fires.

我希望这会有所帮助.我仍在与此作斗争(这是一项巨大的反复试验),但它似乎奏效了.

I hope this helps. I am still battling with this (it is one giant trial and error endeavor), but it seems to be working.

这篇关于如何在 Application 上从 MediaElement 切换到 BackgroundMediaPlayer 失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:33
查看更多