【问题标题】:Running Background Audio in different pages of UWP App在 UWP App 的不同页面运行背景音频
【发布时间】:2016-05-31 06:17:45
【问题描述】:

我正在制作一个 UWP 应用程序,我在主页面中的按钮单击事件上运行背景音频。当我移动到另一个页面时,那里的背景音频任务中也有不同的媒体可以播放。

如何停止当前正在播放的任务来运行另一个?我应该在全球范围内定义一些东西吗?有关此问题的任何帮助?

编辑

我正在使用这个示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundAudio 当第一个页面的 backgroundAudio 正在运行时,我转到第二个页面并在点击事件中使用以下代码设置一个新列表:

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

但是新歌的运行时间超过了预估的时间,进入这个方法的else:

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);
        }

老的(第一次播放的)歌曲继续播放。

我尝试使用 BackgroundMediaPLayer.Shutdown() 停止当前的 BackgroundMediaPlayer,但没有成功。

知道如何让老歌停止播放当前歌曲吗?

【问题讨论】:

  • 你试过 BackgroundMediaPlayer.Current.Pause() 代替吗?

标签: c# uwp live-streaming background-task background-audio


【解决方案1】:

您可以通过从前台向其发送消息来控制后台媒体播放器。例如,

从前台应用程序:

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 将直播 url 作为值传递给后台任务。

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 2020-09-20
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    相关资源
    最近更新 更多