您的位置:首页 > 其它

开发落网电台windows phone 8应用的计划(4)

2014-03-13 22:00 323 查看
昨天把应用的后台播放代理搞定了,但是运行的状况不太好,在网络不好的情况下有时候会崩溃,更多的时候是没有声音。

现在的任务就有两个了:一个是搞定网络,这部分还没有一个很好的计划,因为前台UI和后台播放代理的通信我还没有想到很好的办法,能想到的方法是借助backgroundPlayer的调用来解决,不过这就限制了后期功能的扩展,不过可能就先这么做吧;第二个是怎么处理好缓冲的问题。

先把后台代理放在这上面吧,还是简单版本。参照了csdn的做法,没有做网络。

using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.BackgroundAudio;
using System.Collections.Generic;
using System.Threading;
namespace LuooAudioPlayerAgent
{
public class AudioPlayer : AudioPlayerAgent
{
#region 字段
static int currentTrackNumber=0;
public static int totalNumber;
private static List<AudioTrack> playList=new List<AudioTrack>()
{
new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/01.mp3",UriKind.Absolute),"1","","",null),
new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/02.mp3",UriKind.Absolute),"2","","",null),
//new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/03.mp3",UriKind.Absolute),"","","",null),
//new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/04.mp3" , UriKind.Absolute) , "" , "" , "" , null)
};
#endregion

static AudioPlayer()
{
//GetList();

Deployment.Current.Dispatcher.BeginInvoke(delegate
{
Application.Current.UnhandledException += UnhandledException;
});
}

#region 出现未处理的异常时执行的代码
private static void UnhandledException( object sender , ApplicationUnhandledExceptionEventArgs e )
{
if(Debugger.IsAttached)
{
// 出现未处理的异常;强行进入调试器
Debugger.Break();
}
}
#endregion

protected override void OnPlayStateChanged( BackgroundAudioPlayer player , AudioTrack track , PlayState playState )
{
switch(playState)
{
case PlayState.TrackEnded:
PlayNextTrack(player);
break;
case PlayState.TrackReady:
player.Play();
break;
}
NotifyComplete();
}

protected override void OnUserAction( BackgroundAudioPlayer player , AudioTrack track , UserAction action , object param )
{
switch(action)
{
case UserAction.Play:
if(player.Track==null) player.Track=playList[currentTrackNumber];
player.Play();
break;

case UserAction.Pause:
player.Pause();
break;

case UserAction.SkipNext:
PlayNextTrack(player);
break;

case UserAction.SkipPrevious:
PlayPreviousTrack(player);
break;

//case UserAction.FastForward:
//    GetList();
//    break;
}
NotifyComplete();
}

#region  control

private void PlayNextTrack( BackgroundAudioPlayer player )
{
if(++currentTrackNumber >=playList.Count)
{
currentTrackNumber = 0;
}
player.Track=playList[currentTrackNumber];

}

private void PlayPreviousTrack( BackgroundAudioPlayer player )
{
if(--currentTrackNumber < 0)
{
currentTrackNumber = playList.Count - 1;
}
player.Track=playList[currentTrackNumber];

}

#endregion

protected override void OnError( BackgroundAudioPlayer player , AudioTrack track , Exception error , bool isFatal )
{
if(isFatal)
{
Abort();
}
else
{
NotifyComplete();
}

}

protected override void OnCancel()
{

}

public static void GetList()
{
playList.Add(new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/04.mp3" , UriKind.Absolute) , "4" , "" , "" , null));
//playList.Add(new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/02.mp3" , UriKind.Absolute) , "" , "" , "" , null));
//playList.Add(new AudioTrack(new Uri("http://luoo.800edu.net/low/luoo/radio587/03.mp3" , UriKind.Absolute) , "" , "" , "" , null));
//totalNumber=playList.Count;
}
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: