wp7音乐播放器
前台代码:
<!--layoutroot 是包含所有页面内容的根网格-->
<grid x:name="layoutroot" background="transparent">
<grid.rowdefinitions>
<rowdefinition height="auto"/>
<rowdefinition height="*"/>
</grid.rowdefinitions>
<!--titlepanel 包含应用程序的名称和页标题-->
<stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28">
<textblock fontsize="25" x:name="applicationtitle" text="我的应用程序" style="{staticresource phonetextnormalstyle}"/>
<textblock fontsize="60" x:name="pagetitle" text="播放mediaplayer" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}" width="486" />
</stackpanel>
<!--contentpanel - 在此处放置其他内容-->
<grid x:name="contentpanel" grid.row="1" margin="12,0,12,0">
<image height="216" horizontalalignment="left" margin="35,48,0,0" name="cover" stretch="fill" verticalalignment="top" width="401" />
<textbox height="72" horizontalalignment="left" margin="1,298,0,0" name="songname" text="" verticalalignment="top" width="460" />
<button content="播放" height="72" horizontalalignment="left" margin="6,409,0,0" name="btn_play_music" verticalalignment="top" width="444" click="btn_play_music_click_1" />
<button content="停止" height="72" horizontalalignment="left" margin="12,503,0,0" name="btn_stop" verticalalignment="top" width="438" click="btn_stop_click_1" />
</grid>
</grid>
后台代码:
namespace yinyuebofangqi
{
public partial class mainpage : phoneapplicationpage
{
#region 成员声明
song _playsong; //声明一个song 类型的变量
bool _historyitemlaunch;
const string _playsongkey = "playsong";
#endregion
// constructor
public mainpage()
{
initializecomponent();
_playsong = null;
_historyitemlaunch = false;
//声明一个timer 使用线程运行 xna internals 来自于微软xna框架
dispatchertimer dt = new dispatchertimer();
dt.interval = timespan.frommilliseconds(33);
//使用委托调用逐帧调用 frameworkdispatcher.update();
dt.tick += delegate
{
try
{
// 这个函数作用是更新各种xna状态,并触发相应的事件,这个是必须的,否则会报异常
frameworkdispatcher.update();
}
catch
{
throw;
}
};
dt.start();
mediaplayer.mediastatechanged += new eventhandler<eventargs>(mediaplayer_mediastatechanged);
}
/// <summary>
/// 当页面进入时调用
/// </summary>
/// <param name="e"></param>
protected override void onnavigatedto(system.windows.navigation.navigationeventargs e)
{
medialibrary library = new medialibrary();
if (navigationcontext.querystring.containskey(_playsongkey))
{
string songtoplay = navigationcontext.querystring[_playsongkey];
foreach (song song in library.songs)
{
if (0 == string.compare(songtoplay, song.name))
{
_playsong = song;
}
}
_historyitemlaunch = true;
}
else if (mediaplayer.state == mediastate.playing)
{
_playsong = mediaplayer.queue.activesong;
}
else
{
random rd = new random();
if (library.songs.count > 0)
{
_playsong = library.songs[rd.next(library.songs.count)];
}
else
{
songname.text = "no songs in library";
btn_play_music.isenabled = false;
}
}
base.onnavigatedto(e);
}
void mediaplayer_mediastatechanged(object sender, eventargs e)
{
switch (mediaplayer.state)
{
case mediastate.playing:
btn_play_music.isenabled = false;
btn_stop.isenabled = true;
break;
case mediastate.paused:
case mediastate.stopped:
btn_play_music.isenabled = true;
btn_stop.isenabled = false;
break;
}
}
/// <summary>
/// 为主窗体添加ui加载完成后的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void phoneapplicationpage_loaded(object sender, routedeventargs e)
{
populatesongmetadata();
setinitialbuttonstate();
if (_historyitemlaunch)
{
playsong();
}
}
/// <summary>
/// 开始播放音乐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//private void btn_play_music_click(object sender, routedeventargs e)
//{
// playsong();
//}
///// <summary>
///// 停止播放音乐
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void btn_stop_click(object sender, routedeventargs e)
//{
// stopsong();
//}
/// <summary>
/// 播放
/// </summary>
private void playsong()
{
if (_playsong != null)
{
mediaplayer.play(_playsong);
}
}
/// <summary>
/// 停止
/// </summary>
private void stopsong()
{
if (mediastate.playing == mediaplayer.state)
{
mediaplayer.stop();
}
}
/// <summary>
/// 使用图片读取图片封面
/// </summary>
private void populatesongmetadata()
{
if (_playsong != null)
{
songname.text = _playsong.name;
stream albumartstream = _playsong.album.getalbumart();
if (albumartstream == null)
{
streamresourceinfo albumartplaceholder = application.getresourcestream(new uri("applicationicon.png", urikind.relative));
albumartstream = albumartplaceholder.stream;
}
bitmapimage albumartimage = new bitmapimage();
albumartimage.setsource(albumartstream);
cover.source = albumartimage;
}
}
/// <summary>
/// 初始化按钮
/// </summary>
private void setinitialbuttonstate()
{
switch (mediaplayer.state)
{
case mediastate.playing:
btn_play_music.isenabled = false;
btn_stop.isenabled = true;
break;
case mediastate.paused:
case mediastate.stopped:
btn_play_music.isenabled = true;
btn_stop.isenabled = false;
break;
}
}
private void btn_play_music_click_1(object sender, routedeventargs e)
{
playsong();
}
private void btn_stop_click_1(object sender, routedeventargs e)
{
stopsong();
}
}
}
上一篇: Oracle存储过程游标用法分析
下一篇: jQuery鼠标移动图片上实现放大效果