如何在调用 Close 方法后重用 BackgroundAudioPlayer [英] How to reuse BackgroundAudioPlayer after Close method called

查看:31
本文介绍了如何在调用 Close 方法后重用 BackgroundAudioPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MediaElement 播放视频,使用 BackgroundAudioPlayer 播放音频.

I’m using MediaElement for playing videos and BackgroundAudioPlayer for playing audio.

这是一个案例.

我正在通过 BackgroundAudioPlayer 播放远程音频.然后我想播放视频,在 MediaElement 开始播放视频之前,我按照 BackgroundAudioPlayer 最佳实践中的建议调用 BackgroundAudioPlayer.Close.

I’m playing remote audio via BackgroundAudioPlayer. Then I want to play video and before MediaElement begins playing video I’m calling BackgroundAudioPlayer.Close as suggested in BackgroundAudioPlayer best practices.

MediaElement and the BackgroundAudioPlayer

Care must be taken when mixing BackgroundAudioPlayer and MediaElement for audio playback.

1. Close() must be called before switching to MediaElement playback.

2. There is only one media queue. Your application cannot pause background audio, play something with MediaElement then resume the background audio stream.

但是在播放视频后我想再次播放音频.

But after video is playing I want to play audio again.

// Play audio result
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri(audioSearchResult.Url, UriKind.Absolute), audioSearchResult.Title, null, null, null, 
                AudioPlayer.TrackStateBuffering, EnabledPlayerControls.All);
BackgroundAudioPlayer.Instance.Play();

我在第一行代码中收到 InvalidOperationException,提示背景音频资源不再可用".那么如何在使用 MediaElement 后在我的应用中重用 BackgroundAudioPlayer?

I’m getting InvalidOperationException on first line of code saying "The background audio resources are no longer available". So how can I reuse BackgroundAudioPlayer in my app after using MediaElement?

如果使用 MediaPlayerLauncher 而不是 MediaElement,它会在第二次播放音频时工作,因为当 MediaPlayerLauncher 启动时,应用程序被墓碑化.但是是否可以在一个应用程序中混合 MediaElement 和 BackgroundAudioPlayer!?!?!似乎是 MS 的另一个噩梦:(

If use MediaPlayerLauncher instead of MediaElement it works second time audio being played cause app is being tombstoned when MediaPlayerLauncher launches. But is it possible to mix MediaElement and BackgroundAudioPlayer in one app!?!?! Seems to be another nightmare from MS:(

推荐答案

看起来你在使用 MediaElement 播放视频后可以继续使用后台音频播放器需要调用 BackgroundAudioPlayer.Instance.Close() 在视频结束之后和使用任何其他 BackgroundAudioPlayer 方法之前.

It looks like for you to be able to continue using the background audio player after you used the MediaElement to play video you need to call BackgroundAudioPlayer.Instance.Close() again after the video end and before using any other BackgroundAudioPlayer methods.

您的示例应如下所示:

// Play audio result
BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri(audioSearchResult.Url, UriKind.Absolute), audioSearchResult.Title, null, null, null, AudioPlayer.TrackStateBuffering, EnabledPlayerControls.All);
BackgroundAudioPlayer.Instance.Play();

这篇关于如何在调用 Close 方法后重用 BackgroundAudioPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆