I’m using MediaElement for playing videos and BackgroundAudioPlayer for playing audio.
Here is a case.
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();
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?
EDIT:
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:(
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.
Your example should look like that:
// 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();
You must call BackgroundAudioPlayer.Instance.Close()
BEFORE you start playing the media element. I've tried this in both WP7.1 and WP8 emulators with a simple Background Audio agent (not streaming). Without this call I consistently see InvalidOperationException
s. With it things behave much better.
For instance:
private void ButtonPlayMediaElement(object sender, RoutedEventArgs e)
{
BackgroundAudioPlayer.Instance.Close();
mediaElement.Source = new Uri("http://wpdevpodcast.episodes.s3.amazonaws.com/Episode_093_Were_All_Stickmen.mp3", UriKind.Absolute);
mediaElement.Play();
}
Also:
You are adding a track from your UI, you should really do this in your GetNextTrack
in the background audio agent.
If you want to use both audio and video media content in your application do not mix MediaElement
with BackgroundAudioPlayer
!
Use MediaLauncher
with BackgroundAudioPlayer
and of course do not forget to call BackgroundAudioPlayer.Instance.Close()
before MediaLauncher.Show()
来源:https://stackoverflow.com/questions/13769275/how-to-reuse-backgroundaudioplayer-after-close-method-called