How to reuse BackgroundAudioPlayer after Close method called
Asked Answered
C

3

1

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:(

Conjoin answered 7/12, 2012 at 18:55 Comment(2)
I see that when use MediaPlayerLauncher, I do not need to call Close :)Aggregate
Exact same issue (MediaElement to play video and BackgroundAudioPlayer for audio) and stumbled on this issue after close. Just poorly designed/documented sdk. I guess I will try the MediaPlayeRLauncher and rip up the MediaElement code.. sighSmutchy
A
2

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();
Armor answered 6/3, 2014 at 20:37 Comment(1)
This is the correct answer. It seems like you can use mediaelement and backgroundmediaplayer IF you follow this solution (at least it works in mine. So thank you);Smutchy
I
1

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 InvalidOperationExceptions. 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.

Impassible answered 10/12, 2012 at 12:16 Comment(6)
I do call BackgroundAudioPlayer.Instance.Close() But problem is I cannot mix MediaElement with BackgroundAudioPlayer, because BackgroundAudioPlayer won’t load its dll again after Close called and MediaElement finish its job. So any call to Instance property would cause InvalidOperationException. But it works with MediaPlayerLauncher, because after app is restored, BackgroundAudioPlayer can load its dlls again. So it looks like it’s impossible to mix MediaElement and BackgroundAudioPlayer in same app, only MediaPlayerLauncher and BackgroundAudioPlayer.Conjoin
What happens if you move out the attempt to change the track in the foreground app (move it to the agent)? I tested this in a simple test project on both WP7 and WP8.Impassible
@NazarGrynko calling Close before using MediaElement will clear its resource, and you will have no problem the second time you use BackgroundAudioPlayer, you will see it loads .dll again. I've tested it in my app.Aggregate
I can’t do that! When player has stopped, only user may initiate playing audio from UI in my case.Conjoin
Do it at least as a quick test. In reality if this works, you will need to communicate what track to play from the UI to your background agent. The SDK stops you initiating audio from the background agent, but the background agent should be responsible for selecting what track to play next.Impassible
Now it's two year from this answer, and I also dev in WP8 instead of WP7, but this answer still help me from a big trouble. Thank you very much !!!Sapienza
C
0

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()

Conjoin answered 22/12, 2012 at 15:34 Comment(1)
yes, if you cannot handle those exceptions, using MediaPlayerLauncher is a good choice, I see many apps doing so. And, when using MediaPlayerLauncher, I do not need to call Close(). It just works wellAggregate

© 2022 - 2024 — McMap. All rights reserved.