让AudioPlayerAgent支持动态播放列表(Windows Phone)

最近做的是有关背景音乐播放(调用系统播放器)的活儿,其中牵扯到Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer播放本地或远程音频时候的播放列表改动问题。如果希望播放系统Music + Videos中存储的音乐,那么直接在UI利用Microsoft.Xna.Framework.Media传递和播放音乐即可。但如果是BackgroundAudioPlayer和AudioPlayerAgent就不太好办。

可能有人会说这种问题还用得着单写一篇文章吗,AudioPlayerAgent当然支持动态列表了。但很可惜MSDN的官方How-To的例子就是硬编码的列表(还是本地音频),不然也不会在App Hub Forum上出现这样的问题(12)。

一般来说将播放列表传递给Agent有这几种方式:

  • IsolatedStorageSettings
  • 静态方法或属性(作为BackgroundAgent,实例化它没有意义)
  • IsolatedStorageFile

实验之后就会发现,前两种方法并不好用:虽然在UI线程中设置了列表或者通过静态方法、属性传递了列表,但启动后的Agent拿不到这些数据。就算是单写一个存储播放列表数据的静态类也是一样。那么留给我们的方法只有通过文件传递。

上面提到的那个帖子中,一位微软员工Mark Chamberlain给出的方案也是如此(全文请看原帖):

This is how I did it:
In background audio agent code:
OnUserAction.Play/SkipNext/SkipPrevious: Check if BackgroundAudioPlayer.Instance.Track == null, if yes then load playlist from IsoStorage into m_playlist and start playing first track, if no then play current/next/previous song from m_playlist
In the album viewmodel code (each time a user selects an album a new viewmodel gets created for this app):
OnInitialization:…
Another developer who reviewed this suggested that instead of calling ..Instance.Close(), which also releases all the background audio resources, a better approach is to set BackgroundAudioPlayer.Instance.Track = null, which stops and clears, but keeps the background resources allocated.

基本思路即UI存好文件,Agent初始化时候载入。任何和播放相关操作前播放列表如果为null则再次载入。

具体到我手头的例子,文件格式我直接用的Xml序列化,写起来简单。由于需要和UI保持实时,那么每修改一次播放列表(添加、删除、改动)都要保存,同时在Agent一边,任何和播放相关操作前则不仅检查是否列表为null,而是直接全部重新载入。类似于:

protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param) {
    var allTracks = TrackManager.GetAllTracks();
    _playList = new List();
    foreach ( TrackItem trackItem in allTracks ) {
    _playList.Add( 
            new AudioTrack( null, trackItem.Title, trackItem.Album, null, null, trackItem.Url, EnabledPlayerControls.Pause ) );
    }
    //...
}

代码写出来非常愚蠢,但目前为止找不到更好的方法。

261 Responses to “让AudioPlayerAgent支持动态播放列表(Windows Phone)”

  1. Stromectol说道:

    stromectol 12 mg tablets stromectol price usa buy stromectol online

  2. Stromectol说道:

    stromectol price usa stromectol for humans for sale stromectol price usa

  3. Stromectol说道:

    stromectol for sale stromectol for sale stromectol 12 mg tablets

  4. Stromectol说道:

    prescribing stromectol stromectol price usa stromectol

  5. Stromectol说道:

    prescribing stromectol stromectol 12 mg tablets buy stromectol online

  6. Stromectol说道:

    stromectol for humans for sale stromectol price usa stromectol without a doctor prescription

  7. Stromsiz说道:

    pills erectile dysfunction ed pills online ed drug prices

  8. Stromsiz说道:

    canadian pharmacies not requiring prescription generic drugs without doctor’s prescription prescription drugs without doctor approval

  9. Sildenafil说道:

    viagra pills sildenafil citrate tablets 100 mg sildenafil 100 mg lowest price

  10. Sildenafil说道:

    sildenafil citrate tablets 100 mg sildenafil 20 mg sildenafil 100 mg

  11. DouglasRag说道:

    buy clomid 50mg online where to buy cheap clomid online clomid

  12. Sildenafil说道:

    price for viagra sildenafil 100 mg viagra pills

  13. Sildenafil说道:

    sildenafil 20 mg sildenafil sildenafil

  14. Sildenafil说道:

    price for viagra sildenafil 100 mg lowest price price for viagra

  15. Sildenafil说道:

    sildenafil 100 mg lowest price sildenafil 20 mg sildenafil citrate 100mg for sale

  16. Sildenafil说道:

    viagra where to buy sildenafil 20 mg viagra pills

  17. Sildenafil说道:

    sildenafil 20 mg sildenafil 100 mg sildenafil

  18. Sildenafil说道:

    viagra tablets for men viagra viagra tablets for men

  19. DouglasRag说道:

    cialis from india cialis from india where to buy liquid cialis

  20. Tadalafil说道:

    tadalafil 10mg price tadalafil tablets 20 mg india tadalafil without a doctor’s prescription

  21. Stromectol说道:

    stromectol stromectol price usa stromectol for sale

  22. Clomid说道:

    clomid for sale clomid clomid tablets

  23. DouglasRag说道:

    viagra tablets for men viagra sildenafil citrate tablets 100 mg

  24. Clomid说道:

    clomid tablets for sale clomid tablets clomid for sale

  25. Clomid说道:

    clomid for sale canada buy clomid 50mg clomid tablets

  26. Clomid说道:

    clomid tablets for sale where to buy cheap clomid online buy clomid

  27. Clomid说道:

    clomid tablets for sale buy clomid 50mg online clomid tablets

  28. Clomid说道:

    clomid tablets for sale buy clomid 50mg online where to buy cheap clomid online

  29. Clomid说道:

    clomid tablets clomid tablets for sale clomid tablets

  30. Clomid说道:

    clomid for sale canada where to buy cheap clomid online buy clomid 50mg

  31. Clomid说道:

    clomid for sale canada where to buy cheap clomid online clomid tablets

  32. Clomid说道:

    where to buy cheap clomid online buy clomid clomid tablets

  33. DouglasRag说道:

    cialis tadalafil 10mg price tadalafil tablets 20 mg india

  34. DouglasRag说道:

    clomid tablets clomid tablets for sale clomid

  35. DouglasRag说道:

    cialis coupon cialis pills cialis coupon

  36. Stromectol说道:

    does ivermectin kill lyme disease https://stromectolrate.com/
    ivermectin prostate cancer

  37. Stromectol说道:

    ivermectin budgies https://stromectolrate.com/
    ivermectin apple paste

  38. Eugenearext说道:

    stromectol for sale ivermectin 3 mg dose stromectol for humans for sale

  39. Stromectol说道:

    ivermectin for snake mites https://stromectolrate.com/
    how long before ivermectin works

  40. Stromectol说道:

    ivermectin for horses https://stromectolrate.com/
    ivermectin sheep drench tractor supply

  41. Stromectol说道:

    ivermectin tablet for sale https://stromectolrate.com/
    ivermectin mechanism of action in scabies

  42. Stromectol说道:

    guinea pig ivermectin topical dose https://stromectolrate.com/
    durvet ivermectin paste equine dewormer

  43. Stromectol说道:

    ivermectin for canaries https://stromectolrate.com/
    ivermectin 1 dosage for dogs

  44. Stromectol说道:

    ivermectin dosage for cats https://stromectolrate.com/
    ivermectin for lice on humans

  45. Stromectol说道:

    ivermectin 1.87 dosage for humans https://stromectolrate.com/
    agri mectin ivermectin

  46. Eugenearext说道:

    stromectol 12 mg tablets stromectol for sale worming goats with ivermectin

  47. Eugenearext说道:

    can you get ivermectin over the counter buying ivermectin stromectol 12 mg tablets

  48. Ivermectin说道:

    ivermectin for mange in foxes doctors who prescribe ivermectin online ivermectin coronavirus

  49. RaymondWEw说道:

    buy viagra online usa viagra price of viagra

  50. Viagraus说道:

    mexican viagra best over the counter viagra cheap viagra online