How to reuse BackgroundAudioPlayer after Close method

Im using MediaElement to play video and BackgroundAudioPlayer to play sound.

Here is an example.

I play remote sound through BackgroundAudioPlayer. Then I want to play the video before MediaElement starts playing the video Im calling BackgroundAudioPlayer.Close, as suggested in the best examples of BackgroundAudioPlayer.

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 playing the video, I want to play the sound 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(); 

Im getting an InvalidOperationException in the first line of code saying, "Background audio resources are no longer available." So, how can I reuse BackgroundAudioPlayer in my application after using MediaElement?

EDIT:

If you use MediaPlayerLauncher instead of MediaElement, it works during second time playback, because the application starts when MediaPlayerLauncher starts. But is it possible to mix MediaElement and BackgroundAudioPlayer in one application!?!?! This seems to be another nightmare from MS :(

+1
source share
3 answers

It looks like you could continue to use the background audio player after you used MediaElement to play the video, you need to call BackgroundAudioPlayer.Instance.Close () again after the video ends and before using any other BackgroundAudioPlayer methods.

Your example should look like this:

 // 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(); 
+2
source

You must call BackgroundAudioPlayer.Instance.Close() BEFORE you start playing the media element. I tried this in both WP7.1 and WP8 emulators with a simple background sound agent (not streaming). Without this call, I constantly see InvalidOperationException s. With this, 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 add a track from your user interface, you really have to do this in your GetNextTrack in the background audio agent.

+1
source

If you want to use both audio and video resources in your application, do not mix MediaElement with BackgroundAudioPlayer ! Use MediaLauncher with BackgroundAudioPlayer and of course don't forget to call BackgroundAudioPlayer.Instance.Close() before MediaLauncher.Show()

0
source

Source: https://habr.com/ru/post/971215/


All Articles