How to reuse BackgroundAudioPlayer after Close method called

Deadly 提交于 2019-12-01 13:21:25

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!