版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 PAGE47 / NUMPAGES48基于Android系统的音乐播放器设计与实现源程序.ProjectspMusicPlayer01.android.ide.eclipse.adt.ResourceManagerBuilder.android.ide.eclipse.adt.PreCompilerBuilderorg.eclipse.jdt.core.javabuilder.android.ide.eclipse.adt.ApkBuilder.android.ide.eclipse.adt.AndroidNatureorg.eclipse.jdt.core.javanature.class
2、pathAndroidManifest.xml LocalMpsList.javapackage sled.develop;import java.util.ArrayList;import java.util.List;import sled.develop.service.ListSrv;import sled.model.MpsIterm;import sled.utils.AdapterUtils;import sled.utils.AppConstant;import sled.utils.FileUtils;import android.app.ListActivity;impor
3、t android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.ListView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.SimpleAdapter;public
4、class LocalMpsList extends ListActivityprivate List mp3s =null;private RadioGroup radiogroup ; private RadioButton single ;private RadioButton order ;private RadioButton radom ;private int mode = AppConstant.JUMP_MODE_ORDER ;Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-gene
5、rated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.locallist);single = (RadioButton)findViewById(R.id.single);order = (RadioButton)findViewById(R.id.order);radom = (RadioButton)findViewById(R.id.radom);radiogroup = (RadioGroup)fi
6、ndViewById(R.id.menu);radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) / TODO Auto-generated method stubif(checkedId = single.getId()mode = AppConstant.JUMP_MODE_SINGLE;else if(checkedId = order.getId()mode = Ap
7、pConstant.JUMP_MODE_ORDER;else if(checkedId = radom.getId()mode = AppConstant.JUMP_MODE_RADOM;);Overrideprotected void onListItemClick(ListView l, View v, int position, long id) / TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);/MpsIterm mp3=mp3s.get(position);Intent preinte
8、nt = new Intent();preintent.putExtra(position, position);preintent.putExtra(cmd, AppConstant.JUMP_CMD_PREPARE);preintent.putExtra(mode, mode);preintent.setClass(LocalMpsList.this, ListSrv.class);startService(preintent);/*Intent intent=new Intent();intent.putExtra(mp3, mp3);intent.setClass(this, MpsP
9、layer.class);startActivity(intent);*/onStop();Overrideprotected void onResume() / TODO Auto-generated method stubsuper.onResume();updateListView();Overrideprotected void onStop() / TODO Auto-generated method stubsuper.onStop();finish();private void updateListView()FileUtils fileUtils=new FileUtils()
10、;mp3s=fileUtils.getMp3Files(mp3);if (mp3s=null)/如果本地目录找不到Mp3文件mp3s=new ArrayList();MpsIterm mp3=new MpsIterm();mp3.setName_mp3(没有Mp3文件!);mp3.setSize_mp3(0);mp3s.add(mp3);AdapterUtils au=new AdapterUtils(this,R.layout.mp3info_item,mp3s);SimpleAdapter simpleAdapter=au.buildSimpleAdapter();setListAdapt
11、er(simpleAdapter);ItrActivity.javapackage sled.develop;import android.app.Activity;import android.os.Bundle;public class ItrActivity extends ActivityOverrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.
12、introduction);Overrideprotected void onStop() / TODO Auto-generated method stubsuper.onStop();finish();MpsPlayer.javapackage sled.develop;import sled.develop.service.ListSrv;import sled.develop.service.PlayerSrv;import sled.utils.AppConstant;import android.app.Activity;import android.content.Broadca
13、stReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.media.AudioManager;import android.os.Bundle;import android.os.Handler;import android.view.Menu;import androi
14、d.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.SeekBar;import android.widget.TextView;public class MpsPlayer e
15、xtends Activity implements AppConstant private AudioManager am = null ;private IntentFilter intentFilter=null;private BroadcastReceiver receiver=null;private Intent playIntent=null;private Intent intentnext = null ;private Thread t = null ;private LinearLayout ll = null;private ImageButton last_btn
16、= null ;private ImageButton pause_btn = null ;private ImageButton play_btn = null ;private ImageButton next_btn = null ;private ImageButton list_btn = null ;private SeekBar seekbar = null ;private TextView time_view = null ;private TextView lrc_view = null ;private TextView mp3info_view = null ;priv
17、ate ImageView song_img = null ;private ImageView volbar = null ;private ImageView logo = null ;private int SysMaxVolume , CurVolume;private int vol_id = R.drawable.vol_0,R.drawable.vol_1,R.drawable.vol_2,R.drawable.vol_3,R.drawable.vol_4,R.drawable.vol_5,R.drawable.vol_6,R.drawable.vol_7;private int
18、 logo_id = R.drawable.logo_0,R.drawable.logo_1,R.drawable.logo_2,;int logo_index = 0 ;float downX = 0 ;float downY = 0 ;float upX = 0 ;float upY = 0 ;float relateX ;float relateY ;long tempT = 0 ;long downT = 0 ;long moveT = 0 ;long upT = 0 ;private float CLICK_POINTOR = 80 ;/int count = 0 ;Override
19、protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);ll = (LinearLayout)findViewById(R.id.control_view);last_btn = (ImageButton)findViewById(R.id.last);last_btn.setOnClickListener(new LastOnclickListener();pause_btn = (ImageButton)findVi
20、ewById(R.id.pause);pause_btn.setOnClickListener(new PauseOnclickListener();play_btn = (ImageButton)findViewById(R.id.play);play_btn.setOnClickListener(new PlayOnclickListener();next_btn = (ImageButton)findViewById(R.id.next);next_btn.setOnClickListener(new NextOnclickListener();list_btn = (ImageButt
21、on)findViewById(R.id.list);list_btn.setOnClickListener(new ListOnclickListener();seekbar = (SeekBar)findViewById(R.gressbar);seekbar.setOnSeekBarChangeListener(new SeekBarListener();time_view = (TextView)findViewById(R.id.timeview);lrc_view = (TextView)findViewById(R.id.lrcview);mp3info_view =
22、 (TextView)findViewById(R.id.mp3_info);song_img = (ImageView)findViewById(R.id.img_of_the_song);volbar = (ImageView)findViewById(R.id.volfasebar);logo = (ImageView)findViewById(R.id.logo);Overrideprotected void onResume() super.onResume();am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);Sy
23、sMaxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);receiver=new LrcMessBroadcastReceiver();/两个参数,一个是指明操作,一个是指明过滤registerReceiver(receiver,getIntentFilter();initPlayerSrv();initListSrv();dosomething(null,null);Override public boolean onCreateOptionsMenu(Menu menu) / TODO Auto-generated met
24、hod stub menu.add(0, 1, 1, 操作说明); menu.add(0, 2, 2, 完全退出); return super.onCreateOptionsMenu(menu); Override public boolean onOptionsItemSelected(MenuItem item) / TODO Auto-generated method stub if(item.getItemId() = 1) Intent tintent = new Intent(); tintent.setClass(MpsPlayer.this, ItrActivity.class
25、); startActivity(tintent); else if(item.getItemId() = 2) stopService(playIntent); stopService(intentnext); finish(); return true; Overrideprotected void onRestart() super.onRestart();protected void onPause() super.onPause();unregisterReceiver(receiver);/一些基本布局public void dosomething(String mp3info,S
26、tring img)Bitmap bm ;if(mp3info = null | img = null )mp3info = NO MUSIC SELECTED;song_img.setImageResource(R.drawable.logo_0);mp3info_view.setText(mp3info);else/System.out.println(img);mp3info = 当前播放: + mp3info;bm = BitmapFactory.decodeFile(/sdcard/mp3/img/ + img);if(bm = null)song_img.setImageResou
27、rce(R.drawable.logo_0);elsesong_img.setImageBitmap(bm);mp3info_view.setText(mp3info);/* * 一些按钮和进度条的监听方法 * author Administrator * */class LastOnclickListener implements OnClickListenerOverridepublic void onClick(View v) lastMusic();class PauseOnclickListener implements OnClickListenerOverridepublic v
28、oid onClick(View v) pauseMp3();/t.start();class PlayOnclickListener implements OnClickListenerOverridepublic void onClick(View v) initPlayerSrv();playMp3();class NextOnclickListener implements OnClickListenerOverridepublic void onClick(View v) nextMusic();public void initListSrv()intentnext = new In
29、tent();intentnext.setClass(MpsPlayer.this, ListSrv.class);public void nextMusic()intentnext.putExtra(cmd, AppConstant.JUMP_CMD_NEXT);startService(intentnext);public void lastMusic()Intent intentnext = new Intent();intentnext.putExtra(cmd, AppConstant.JUMP_CMD_LAST);intentnext.setClass(MpsPlayer.this
30、, ListSrv.class);startService(intentnext);class SeekBarListener implements SeekBar.OnSeekBarChangeListener/进度条事件响应public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) public void onStartTrackingTouch(SeekBar arg0) public void onStopTrackingTouch(SeekBar arg0) if (seekbar.getMax() 0)/是
31、一次点击事件if(Math.abs(relateX) = CLICK_POINTOR & Math.abs(relateY) 500)Intent intent = new Intent();intent.setClass(MpsPlayer.this, LocalMpsList.class);startActivity(intent);elsell.setVisibility(View.VISIBLE);t = new Thread(tr);t.start();else if(Math.abs(relateX) Math.abs(relateY)if(downX upX)nextMusic(
32、);else if(downX upX)lastMusic();else if(Math.abs(relateX) Math.abs(relateY)if(downY upY)turnvol(AudioManager.ADJUST_RAISE);upT = -1 ;/upX = upY = 0 ;return super.onTouchEvent(event);/代码复用,关于音量调节public void turnvol(int value)am.adjustStreamVolume (AudioManager.STREAM_MUSIC, value,0);CurVolume = am.ge
33、tStreamVolume(AudioManager.STREAM_MUSIC);int num_pic = (7*CurVolume)/SysMaxVolume ;volbar.setImageResource(vol_idnum_pic);volbar.setVisibility(View.VISIBLE);Thread thread = new Thread(tv);thread.start();/* * 音量条延迟线程 */Handler vhandler = new Handler();Runnable tv = new Runnable()Overridepublic void r
34、un() try Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace();vhandler.post(vr);Runnable vr = new Runnable()Overridepublic void run() volbar.setVisibility(View.GONE);/* * 进度条等按钮延迟线程 */Handler handler = new Handler();Runnable tr = new Runnable()Overridepublic void run() try Thread.s
35、leep(5000); catch (InterruptedException e) e.printStackTrace();handler.post(r);Runnable r = new Runnable() Overridepublic void run() ll.setVisibility(View.GONE);ListSrv.javapackage sled.develop.service;import java.util.ArrayList;import java.util.Random;import sled.model.MpsIterm;import sled.utils.Ap
36、pConstant;import sled.utils.FileUtils;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class ListSrv extends Service implements AppConstantprivate FileUtils files ;private Intent intentplay = null ;private MpsIterm mp3_next = null ;private MpsIterm mp3_last =
37、 null ;private int cmd = AppConstant.JUMP_CMD_PREPARE;private int mode = AppConstant.JUMP_MODE_ORDER ;private int position = -1 ;private int nextp = 0 ;private int lastp = 0 ;private ArrayList list = null ;Overridepublic IBinder onBind(Intent intent) / TODO Auto-generated method stubreturn null;Over
38、ridepublic void onDestroy() / TODO Auto-generated method stubsuper.onDestroy();Overridepublic int onStartCommand(Intent intent, int flags, int startId) / TODO Auto-generated method stubcmd = intent.getIntExtra(cmd, AppConstant.JUMP_CMD_PREPARE);switch(cmd)case AppConstant.JUMP_CMD_PREPARE:files = ne
39、w FileUtils();list = (ArrayList)files.getMp3Files(mp3);mode = intent.getIntExtra(mode, AppConstant.JUMP_MODE_ORDER);/System.out.println(+ mode);position = intent.getIntExtra(position, position);initPlayerSrv();intentplay.putExtra(mp3, list.get(position);startService(intentplay);whatsnext(mode,positi
40、on);break;case AppConstant.JUMP_CMD_NEXT:if(position 0)return 0;initPlayerSrv();intentplay.putExtra(mp3, mp3_next);startService(intentplay);position = nextp ;whatsnext(mode,position);break;case AppConstant.JUMP_CMD_LAST:if(position list.size() - 1 )nextp = 0 ;if( lastp 停止 + mp3.getName_mp3()+ 的PlayS
41、rv!);stop();mp3=null;mp=null;mediaStatus=AppConstant.MEDIA_STATUS_UNLOAD;mediaCmd=AppConstant.MEDIA_CMD_STOP;super.onDestroy();Overridepublic int onStartCommand(Intent intent, int flags, int startId) / TODO Auto-generated method stubstartTime=intent.getIntExtra(startTime, 0);MpsIterm mp3InfoNew=(Mps
42、Iterm) intent.getSerializableExtra(mp3);/判断Activity传过来的 mp3Name 是否同一个,不同则停止原有的,再启动新的播放.if (mp3!=null & mp3InfoNew != null)String str1=mp3.getName_mp3();String str2=mp3InfoNew.getName_mp3();boolean isEqual=str1.equals(str2);if (!isEqual)stop();if(mp3InfoNew != null)mp3=mp3InfoNew;if(mp3 = null)return
43、 0 ;mp3info = mp3.getName_mp3();img = mp3.getName_img();mediaCmd=intent.getIntExtra(mediaCmd, AppConstant.MEDIA_CMD_PLAY);if (mediaCmd=AppConstant.MEDIA_CMD_PLAY)play();else if (mediaCmd=AppConstant.MEDIA_CMD_PAUSE)pause();else if (mediaCmd=AppConstant.MEDIA_CMD_GOTO)stop();mediaStatus = AppConstant
44、.MEDIA_STATUS_RELEASED;currLrcIndex = 0 ;play();else if (mediaCmd=AppConstant.MEDIA_CMD_SEEKTO)seekto();else if (mediaCmd=AppConstant.MEDIA_CMD_STOP)stop();return super.onStartCommand(intent, flags, startId);/播放器操作private void play()boolean isMtach=true;if (mediaStatus=AppConstant.MEDIA_STATUS_UNLOA
45、D) | (mediaStatus=AppConstant.MEDIA_STATUS_RELEASED)isMtach= initMediaPlayerAndLrc();else if(mediaStatus=AppConstant.MEDIA_STATUS_PLAYING)isMtach=false;/通知Activity设置进度条,因为ACT中SeekBar在mp3player.xml里面,max的值配置为0,totalTimeTxt也是配置为零./因此在同一首歌的播放中,退出Mp3PlayerActivity后又重新回到Mp3PlayerActivity的时候,因为Mp3PlayerAc
46、tivity的onCreate/事件中,调用setContentView设置SeekBar和totalTimeTxt为mp3player里面的值,即为0,所以需要在这里手工设置一下.sendBroadcastToAct(AppConstant.UI_CMD_INITPROGRESSBAR,0,mp.getDuration();if (isMtach)mp.start();/if(updateTimeCallback=null)/sendBroadcastToAct(AppConstant.UI_CMD_UPDATELRC,找不到歌词文件!,0,0);/elsehandler.post(upda
47、teTimeCallback);/mediaStatus=AppConstant.MEDIA_STATUS_PLAYING;handler.postDelayed(updateTimeCallback, 5);private void pause()if (mediaStatus=AppConstant.MEDIA_STATUS_PLAYING) mp.pause();mediaStatus=AppConstant.MEDIA_STATUS_PAUSE;else if(mediaStatus=AppConstant.MEDIA_STATUS_PAUSE)mp.start();mediaStat
48、us=AppConstant.MEDIA_STATUS_PLAYING;private void seekto()if (mp=null)return;if (startTime=0) return;/歌词数组的索引号从0开始查找currLrcIndex=0;mp.seekTo(startTime);/play();mp.start();startTime=0;return ;private void stop()if (mediaStatus=AppConstant.MEDIA_STATUS_PLAYING) | (mediaStatus=AppConstant.MEDIA_STATUS_P
49、AUSE)/nu.viewNotification(mp3.getName_mp3() + 停 止!, mp3.getName_mp3() + 停止!, R.drawable.icon);handler.removeCallbacks(updateTimeCallback);mp.stop();mp.release();mediaStatus=AppConstant.MEDIA_STATUS_RELEASED;/发送通知,清除播放信息(歌词,进度条)sendBroadcastToAct(AppConstant.UI_CMD_CLEARPLAYINFO,0,0);/自定义函数(1):设置Medi
50、aPlayerprivate boolean initMediaPlayerAndLrc()String SDPath=Environment.getExternalStorageDirectory().getAbsolutePath();String mp3Path=SDPath +/mp3/ + mp3.getName_mp3();prepareLrc(mp3.getName_lrc();updateTimeCallback = new UpdateTimeCallback();/要用MediaPlayer.create 而不是 mp.createmp=MediaPlayer.create
51、(PlayerSrv.this, Uri.parse(file:/ + mp3Path);mp.setLooping(false);handler=new Handler(); /显示歌词用的handler/通知Activity设置进度条sendBroadcastToAct(AppConstant.UI_CMD_INITPROGRESSBAR,0,mp.getDuration();return true;/自定义函数(2):准备歌词private void prepareLrc(String lrcName)tryString SDPath=Environment.getExternalSto
52、rageDirectory().getAbsolutePath();String lrcPath=SDPath +/mp3/lrc/ + lrcName;InputStream inputStream=new FileInputStream(lrcPath);LrcPrcsUtils lrcProcessor=new LrcPrcsUtils();lrcPcess(inputStream);arrMessages=lrcProcessor.getArrMsgs();arrTimes=lrcProcessor.getArrTimes();/updateTimeCallba
53、ck = new UpdateTimeCallback();/nextTimeMill =0;catch(FileNotFoundException e) e.printStackTrace();return ;/自定义函数(3):发送广播private void sendBroadcastToAct(int uiCmd,String lrcMess,long currTime,int duration)Intent broadcastIntent=new Intent();if (uiCmd=0)return;broadcastIntent.setAction(AppConstant.LRC
54、_MESSAGE_ACTION);broadcastIntent.putExtra(uiCmd, uiCmd);if (lrcMess!=null) broadcastIntent.putExtra(lrcMess,lrcMess);if (currTime0) broadcastIntent.putExtra(currPos, currTime);if (duration0) broadcastIntent.putExtra(duration,duration);if (mp3info != null) broadcastIntent.putExtra(mp3info, mp3info);i
55、f (img != null ) broadcastIntent.putExtra(img, img);sendBroadcast(broadcastIntent);return;class UpdateTimeCallback implements Runnablepublic UpdateTimeCallback() super();public void run() / TODO Auto-generated method stubtry/直接用MediaPlayer中的变量得到播方时间Long offset=(long) mp.getCurrentPosition();boolean
56、isMatch =false;if(arrMessages != null)if (currLrcIndex=0) /当歌词数组索引号为0时,显示与播放时间最接近的歌词.for (int i = currLrcIndex; i = offset)currLrcIndex=i;isMatch=true;System.out.println(here is working);break;else for (int i = currLrcIndex; i offset)isMatch=true;currLrcIndex=i;break;if (isMatch)/改成用广播的方式更新歌词if(curr
57、LrcIndex = mp.getDuration()stop();Intent intent = new Intent();intent.putExtra(cmd, AppConstant.JUMP_CMD_NEXT);intent.setClass(PlayerSrv.this, ListSrv.class);startService(intent);catch (Exception e)e.printStackTrace();handler.postDelayed(updateTimeCallback,10);MpsIterm.javapackage sled.model;import
58、java.io.Serializable;public class MpsIterm implements Serializableprivate String name_mp3;private long size_mp3;private String name_lrc;private String name_img;public MpsIterm()public MpsIterm(String name_mp3,long size_mp3,String name_lrc,String name_singer,String name_img)_mp3 = name_mp3;t
59、his.size_mp3 = size_mp3;_lrc = name_lrc;_img = name_img;public String getName_mp3() return name_mp3;public void setName_mp3(String name_mp3) _mp3 = name_mp3;public long getSize_mp3() return size_mp3;public void setSize_mp3(long size_mp3) this.size_mp3 = size_mp3;public Str
60、ing getName_lrc() return name_lrc;public void setName_lrc(String name_lrc) _lrc = name_lrc;public String getName_img() return name_img;public void setName_img(String name_img) _img = name_img;Overridepublic String toString() return MpsIterm name_mp3= + name_mp3 + , size_mp3= + size
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Linux系统管理及应用项目式教程课件 项目6 管理软件包与进程
- 班主任工作范文小学班主任工作计划三
- 高二上学期数学人教A版(2019)期末模拟测试卷A卷(含解析)
- 网络安全基础技术应用知到智慧树章节测试课后答案2024年秋深圳信息职业技术学院
- 《福利国家》课件
- 2025届福建闽侯第六中学高三最后一卷数学试卷含解析
- 广东普宁华侨中学2025届高考语文一模试卷含解析
- 两个矩阵的等价、相似、合同的关系
- 河南省舞钢市第二高级2025届高三第二次模拟考试英语试卷含解析
- 放弃治疗协议书 篇三
- 人音版六年级音乐上册期末试卷(含答案)
- 医院科室医疗质量安全管理持续改进记录本(包括活动记录与科室质控数据监测与统计表格)(模板)
- 停车场利润分成协议
- 家具安装垃圾清理方案
- 外研社版三起英语四年级上册复习提纲-
- 《有创监测技术》课件
- 马工程版《中国经济史》各章思考题答题要点及详解
- 人工智能在网络安全的应用详述
- 【课件】信息系统的优势与局限性 2023-2024学年人教中图版(2019)高中信息技术必修二
- 资产评估公司三级复核管理制度
- 国开《植物病虫害防治基础》形考任务1-4答案
评论
0/150
提交评论