开发落网电台windows phone 8应用的计划(4)
昨天把应用的后台播放代理搞定了,但是运行的状况不太好,在网络不好的情况下有时候会崩溃,更多的时候是没有声音。
现在的任务就有两个了:一个是搞定网络,这部分还没有一个很好的计划,因为前台UI和后台播放代理的通信我还没有想到很好的办法,能想到的方法是借助backgroundPlayer的调用来解决,不过这就限制了后期功能的扩展,不过可能就先这么做吧;第二个是怎么处理好缓冲的问题。
先把后台代理放在这上面吧,还是简单版本。参照了csdn的做法,没有做网络。
1 using System; 2 using System.Diagnostics; 3 using System.Windows; 4 using Microsoft.Phone.BackgroundAudio; 5 using System.Collections.Generic; 6 using System.Threading; 7 namespace LuooAudioPlayerAgent 8 { 9 public class AudioPlayer : AudioPlayerAgent 10 { 11 #region 字段 12 static int currentTrackNumber=0; 13 public static int totalNumber; 14 private static List<AudioTrack> playList=new List<AudioTrack>() 15 { 16 new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/01.mp3",UriKind.Absolute),"1","","",null), 17 new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/02.mp3",UriKind.Absolute),"2","","",null), 18 //new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/03.mp3",UriKind.Absolute),"","","",null), 19 //new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/04.mp3" , UriKind.Absolute) , "" , "" , "" , null) 20 }; 21 #endregion 22 23 static AudioPlayer() 24 { 25 //GetList(); 26 27 Deployment.Current.Dispatcher.BeginInvoke(delegate 28 { 29 Application.Current.UnhandledException += UnhandledException; 30 }); 31 } 32 33 #region 出现未处理的异常时执行的代码 34 private static void UnhandledException( object sender , ApplicationUnhandledExceptionEventArgs e ) 35 { 36 if(Debugger.IsAttached) 37 { 38 // 出现未处理的异常;强行进入调试器 39 Debugger.Break(); 40 } 41 } 42 #endregion 43 44 protected override void OnPlayStateChanged( BackgroundAudioPlayer player , AudioTrack track , PlayState playState ) 45 { 46 switch(playState) 47 { 48 case PlayState.TrackEnded: 49 PlayNextTrack(player); 50 break; 51 case PlayState.TrackReady: 52 player.Play(); 53 break; 54 } 55 NotifyComplete(); 56 } 57 58 protected override void OnUserAction( BackgroundAudioPlayer player , AudioTrack track , UserAction action , object param ) 59 { 60 switch(action) 61 { 62 case UserAction.Play: 63 if(player.Track==null) player.Track=playList[currentTrackNumber]; 64 player.Play(); 65 break; 66 67 case UserAction.Pause: 68 player.Pause(); 69 break; 70 71 case UserAction.SkipNext: 72 PlayNextTrack(player); 73 break; 74 75 case UserAction.SkipPrevious: 76 PlayPreviousTrack(player); 77 break; 78 79 //case UserAction.FastForward: 80 // GetList(); 81 // break; 82 } 83 NotifyComplete(); 84 } 85 86 #region control 87 88 private void PlayNextTrack( BackgroundAudioPlayer player ) 89 { 90 if(++currentTrackNumber >=playList.Count) 91 { 92 currentTrackNumber = 0; 93 } 94 player.Track=playList[currentTrackNumber]; 95 96 } 97 98 private void PlayPreviousTrack( BackgroundAudioPlayer player ) 99 { 100 if(--currentTrackNumber < 0) 101 { 102 currentTrackNumber = playList.Count - 1; 103 } 104 player.Track=playList[currentTrackNumber]; 105 106 } 107 108 109 110 #endregion 111 112 113 protected override void OnError( BackgroundAudioPlayer player , AudioTrack track , Exception error , bool isFatal ) 114 { 115 if(isFatal) 116 { 117 Abort(); 118 } 119 else 120 { 121 NotifyComplete(); 122 } 123 124 } 125 126 protected override void OnCancel() 127 { 128 129 } 130 131 public static void GetList() 132 { 133 playList.Add(new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/04.mp3" , UriKind.Absolute) , "4" , "" , "" , null)); 134 //playList.Add(new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/02.mp3" , UriKind.Absolute) , "" , "" , "" , null)); 135 //playList.Add(new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/03.mp3" , UriKind.Absolute) , "" , "" , "" , null)); 136 //totalNumber=playList.Count; 137 } 138 } 139 }