在UWP应用程序的不同页面运行的后台音频

在UWP应用程序的不同页面运行的后台音频

问题描述:

我想提出一个UWP应用程序,我在一个按钮单击事件中运行的MainPage背景音频。当我移动到另一个网页,还有一个不同的传媒背景音频任务那里踢球。

I am making a UWP App where I run Background Audio in the MainPage on a Button Click event. When I move to another page, there's also a different Media to play in Background Audio Task there.

如何停止当前播放的任务运行的其他?我应该在全球范围界定的东西吗? ?关于这个问题的任何帮助。

How can I stop the currently playing Task to run the other? Should I define something globally? Any help regarding this issue?

修改

我使用这个示例: https://github.com/Microsoft/Windows-universal-samples /树/主/样品/ backgroundAudio 虽然第一页是运行的backgroundAudio,我去到第二页和我将用下面的代码一个新的列表中单击事件:

I am using this sample: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundAudio While the backgroundAudio of the first Page is running, I go to the second page and on a click event I set a new List with the following code:

   // First update the persisted start track
   ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.TrackId, RadioFacade.mySongs[0].MediaUri.ToString()); //here
           ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.Position, new TimeSpan().ToString());

                // Start task
                StartBackgroundAudioTask();



但是,新的歌曲的时间比预计时间更运行,并进入该方法的其他:

But the new song takes more than the estimated time to run and enter the else of this method:

private void StartBackgroundAudioTask()
        {
            AddMediaPlayerEventHandlers();

            var startResult = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                bool result = backgroundAudioTaskStarted.WaitOne(10000);
                //Send message to initiate playback
                if (result == true)
                {
                    MessageService.SendMessageToBackground(new UpdatePlaylistMessage(RadioFacade.mySongs));
                    MessageService.SendMessageToBackground(new StartPlaybackMessage());
                }
                else
                {
                    throw new Exception("Background Audio Task didn't start in expected time");
                }
            });
            startResult.Completed = new AsyncActionCompletedHandler(BackgroundTaskInitializationCompleted);
        }

和旧的(第一弹)的歌曲一直播放。

and the old (first playing) song keeps playing.

我试着用停止当前BackgroundMediaPlayer BackgroundMediaPLayer.Shutdown(),但没有奏效。

I tried to Stop the current BackgroundMediaPlayer using BackgroundMediaPLayer.Shutdown() but it didn't work.

不知道如何让老松站和当前歌曲播放?

Any idea how to let the old song stop and the current song play?

可以通过从前景发送消息到它控制背景媒体播放器。例如,

You can control the background media player by sending messages to it from the foreground. For example,

从前台应用程序:

BackgroundMediaPlayer.SendMessageToBackground(new ValueSet
{
    {"playnew", "some value"}
});

在你的后台任务:

public sealed class AudioPlayer : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
        ...
        ...
    }

    private async void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
    {
        object value;
        if (e.Data.TryGetValue("playnew", out value) && value != null)
        {
            // value will be whatever you pass from the foreground.
            BackgroundMediaPlayer.Current.Pause();
            BackgroundMediaPlayer.Current.Source = stream source;
            BackgroundMediaPlayer.Current.Play();
        }
    }

    ...
}

playnew可以在你的应用程序中的全局常量。您可以使用ValueSet传递现场直播网址后台任务作为值。

"playnew" can be a global constant in your application. You can use the ValueSet to pass the live stream url to the background task as the value.