您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 传媒/媒体 > 安卓Android广播、线程、服务与多媒体开发实验报告
实验报告(2013/2014学年第一学期)课程名称软件技术系列课程(Android基础)实验名称Android广播、线程、服务与多媒体开发实验时间2013年12月9日指导单位计算机学院、诚迈科技(南京)公司指导教师学生姓名班级学号学院(系)计算机学院专业软件工程(计算机软件与服务外包)成绩批阅人王彦日期2013-12-091实验报告实验名称Android广播、线程、服务与多媒体开发指导教师实验类型实验实验学时4实验时间2013-12-9一、实验目的和要求验证Broadcast的发送与接收验证多线程间的数据传递验证Service的使用方法验证多媒体开发中音频、视频、或图像的采集与播放编写一个Android小程序要求:1、使用广播传递数据或信息2、使用多线程3、适当应用Service4、实现至少一种多媒体功能二、实验环境(实验设备)PC(Window7)、Eclipse2三、实验原理及内容在这个程序中涉及到了Receiver和BroadcastReceiver的结合使用,首先在AndroidManifest.xml中注册Receiver,实现能够在后台播放音乐的功能。通过CommandReceiver类实现接受前台Activity发送的播放命令的Intent。在CommandReceiver类中,有后台Service注册的接受前台控制命令Intent的Receiver,广播播放状态变化的Intent代码publicclassMusicServiceextendsServiceimplementsRunnable{privateMediaPlayerplayer;privateListMusiclists;publicstaticint_id=1;//当前播放位置publicstaticBooleanisRun=true;publicLrcProcessmLrcProcess;publicLrcViewmLrcView;publicstaticintplaying_id=0;publicstaticBooleanplaying=false;@OverridepublicIBinderonBind(Intentarg0){//TODOAuto-generatedmethodstubreturnnull;}@OverridepublicvoidonCreate(){//TODOAuto-generatedmethodstublists=MusicList.getMusicData(getApplicationContext());SeekBarBroadcastReceiverreceiver=newSeekBarBroadcastReceiver();IntentFilterfilter=newIntentFilter(cn.com.karl.seekBar);this.registerReceiver(receiver,filter);newThread(this).start();super.onCreate();}@OverridepublicvoidonStart(Intentintent,intstartId){3//TODOAuto-generatedmethodstubStringplay=intent.getStringExtra(play);_id=intent.getIntExtra(id,1);if(play.equals(play)){if(null!=player){player.release();player=null;}playMusic(_id);}elseif(play.equals(pause)){if(null!=player){player.pause();}}elseif(play.equals(playing)){if(player!=null){player.start();}else{playMusic(_id);}}elseif(play.equals(replaying)){}elseif(play.equals(first)){intid=intent.getIntExtra(id,0);playMusic(id);}elseif(play.equals(rewind)){intid=intent.getIntExtra(id,0);playMusic(id);}elseif(play.equals(forward)){intid=intent.getIntExtra(id,0);playMusic(id);}elseif(play.equals(last)){intid=intent.getIntExtra(id,0);playMusic(id);}}privatevoidplayMusic(intid){/////////////////////////初始化歌词配置/////////////////////////mLrcProcess=newLrcProcess();//读取歌词文件4mLrcProcess.readLRC(lists.get(_id).getUrl());//传回处理后的歌词文件lrcList=mLrcProcess.getLrcContent();MusicActivity.lrc_view.setSentenceEntities(lrcList);//切换带动画显示歌词MusicActivity.lrc_view.setAnimation(AnimationUtils.loadAnimation(MusicService.this,R.anim.alpha_z));//启动线程mHandler.post(mRunnable);/////////////////////////初始化歌词配置/////////////////////////if(null!=player){player.release();player=null;}if(id=lists.size()-1){_id=lists.size()-1;}elseif(id=0){_id=0;}Musicm=lists.get(_id);Stringurl=m.getUrl();UrimyUri=Uri.parse(url);player=newMediaPlayer();player.reset();player.setAudioStreamType(AudioManager.STREAM_MUSIC);try{player.setDataSource(getApplicationContext(),myUri);player.prepare();}catch(IllegalArgumentExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(SecurityExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IllegalStateExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}player.start();5player.setOnCompletionListener(newOnCompletionListener(){@OverridepublicvoidonCompletion(MediaPlayermp){//TODOAuto-generatedmethodstub//下一首if(MusicActivity.isLoop==true){player.reset();Intentintent=newIntent(cn.com.karl.completion);sendBroadcast(intent);_id=_id+1;playMusic(_id);}else{//单曲播放player.reset();Intentintent=newIntent(cn.com.karl.completion);sendBroadcast(intent);playMusic(_id);}}});player.setOnErrorListener(newOnErrorListener(){@OverridepublicbooleanonError(MediaPlayermp,intwhat,intextra){//TODOAuto-generatedmethodstubif(null!=player){player.release();player=null;}Musicm=lists.get(_id);Stringurl=m.getUrl();UrimyUri=Uri.parse(url);player=newMediaPlayer();player.reset();player.setAudioStreamType(AudioManager.STREAM_MUSIC);try{player.setDataSource(getApplicationContext(),myUri);player.prepare();}catch(IllegalArgumentExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(SecurityExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();6}catch(IllegalStateExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}player.start();returnfalse;}});}privateclassSeekBarBroadcastReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Contextcontext,Intentintent){//TODOAuto-generatedmethodstubintseekBarPosition=intent.getIntExtra(seekBarPosition,0);//System.out.println(--------+seekBarPosition);player.seekTo(seekBarPosition*player.getDuration()/100);player.start();}}@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubwhile(isRun){try{Thread.sleep(200);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}if(null!=player){intposition=player.getCurrentPosition();inttotal=player.getDuration();Intentintent=newIntent(cn.com.karl.progress);intent.putExtra(position,position);intent.putExtra(total,total);7sendBroadcast(inte
本文标题:安卓Android广播、线程、服务与多媒体开发实验报告
链接地址:https://www.777doc.com/doc-7009052 .html