6

我有一个使用BackgroundAudioPlayer.

我需要音频播放代理中的计时器,它将更新从 Internet 广播电台的 API 中提取的 BAP 的当前播放曲目的曲目标题。

将 a 添加DispatcherTimer到音频播放代理中会给我一个跨线程异常,并使用:

Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Code
            });

没用。

我需要这里的代码,因为如果我将更新代码放在应用程序本身中,当用户离开应用程序时更新停止(与 Windows 8 的行为非常不同)。

我不能使用计划代理,因为它们每 30 分钟运行一次(IIRC)。

这是可能的还是不能在 Windows Phone 上完成?

4

3 回答 3

0

以下是背景音频播放器的 MSDN 文档摘录:

在任务之间发送消息:有时您会希望在后台音频应用程序的两个进程之间进行通信。例如,您可能希望后台任务在新曲目开始播放时通知前台任务,然后将新歌曲标题发送到前台任务以显示在屏幕上。一个简单的通信机制会在前台和后台进程中引发事件。SendMessageToForeground 和 SendMessageToBackground 方法各自调用相应任务中的事件。数据可以作为参数传递给接收任务中的事件处理程序。使用名为 ValueSet 的新类传递数据。此类是一个字典,其中包含作为键的字符串和作为值的其他值类型。您可以传递简单的值类型,例如 int、string、bool 等。

https://msdn.microsoft.com/en-US/library/windows/apps/xaml/dn642090

希望这可以帮助!

于 2016-01-05T09:32:34.250 回答
0

我发现了一个可以帮助您的问题:如何在 windows phone 8 的后台运行计时器?

当您设置一个计时器时,如果“标题”与上次已知的标题不同,则每隔 x 秒检查一次,那么您可以将此信息发送回给它。

这可能是计时器的代码:

声明这些:

string _newValue = string.Empty;
string _currentValue = string.Empty;
AudioTrack _tempTrack = null;

并将其设置为计时器的刻度

if (this.BackgroundAudioPlayer != null)
{
   if (this.BackgroundAudioPlayer.Instance != null)
   {
       if (this.BackgroundAudioPlayer.Instance.Track != null)
       {
           this._newValue= yourAPI.GetTitleOfTrack();

           try
           {
               /* First try to get the current Track as own Var */
               this._tempTrack = this.BackgroundAudioPlayer.Instance.Track;
               if (this._tempTrack != null)
               {
                  /* Then Read the .Tag Value from it, save to _currentValue */
                  if (this._tempTrack.Tag != null) 
                  { this._currentValue = this._tempTrack.Tag.ToString(); }
                  else
                  { this._currentValue = string.Empty; }

                  /* Compare */
                  if (this._currentValue != this._newValue)
                  {
                     /* Edit the Track Tag from your original BAP */
                     this.BackgroundAudioPlayer.Instance.Track.Tag = this._newValue;
                  }
               }
           }
           catch(Exception ex)
           {
               /* if something Crashes you can save the exception error for protocol */
           }
       }
   }
}

请记住:使用 API 的真实函数调用更改“yourAPI.GetTitleOfTrack()”-Function。

于 2016-01-08T13:02:35.117 回答
-1

您是否考虑过在跟踪标签中更新后台音频播放器代理中的信息,如下所示。

string newTag = "whatever you need to show";
AudioTrack track = BackgroundAudioPlayer.Instance.Track;
track.BeginEdit();
track.Tag = newTag;
track.EndEdit();

然后在需要时由您的应用程序在前端读取该标签?

于 2014-06-11T09:15:26.970 回答