您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 安卓音乐播放器实现代码
1.Mp3Player.java文件packagecom.jacky.multimedia;importjava.io.File;importjava.io.FilenameFilter;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importandroid.app.ListActivity;importandroid.media.MediaPlayer;importandroid.media.MediaPlayer.OnCompletionListener;importandroid.os.Bundle;importandroid.os.Handler;importandroid.view.KeyEvent;importandroid.view.View;importandroid.widget.ArrayAdapter;importandroid.widget.ImageButton;importandroid.widget.ListView;importandroid.widget.SeekBar;importandroid.widget.SeekBar.OnSeekBarChangeListener;importandroid.widget.TextView;publicclassMp3PlayerextendsListActivity{/**Calledwhentheactivityisfirstcreated.*//*定义5个图像按钮*/privateImageButtonmFrontImageButton=null;privateImageButtonmStartImageButton=null;privateImageButtonmPauseImageButton=null;privateImageButtonmStopImageButton=null;privateImageButtonmNextImageButton=null;/*定义进度handler,显示百分比进度*/HandlermPercentHandler=newHandler();/*定义拖动条*/privateSeekBarmSeekBar=null;/*定义显示文本框*/privateTextViewcurProgressText=null;privateTextViewcurtimeAndTotaltime=null;/*定于一个多媒体对象*/publicMediaPlayermMediaPlayer=null;/*定于一个数据播放列表,用来存放从指定文件中搜索到的文件*/privateListStringmMusicList=newArrayListString();/*定义在播放列表中的当前选择项*/privateintcurrentListItme=0;/*定义要播放的文件夹路径*/privatestaticfinalStringMUSIC_PATH=newString(/mnt/sdcard/);/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);/*更新播放列表*/musicList();/*初始化多媒体对象*/mMediaPlayer=newMediaPlayer();/*初始化图像按钮*/mFrontImageButton=(ImageButton)findViewById(R.id.LastImageButton);mStopImageButton=(ImageButton)findViewById(R.id.StopImageButton);mStartImageButton=(ImageButton)findViewById(R.id.StartImageButton);mPauseImageButton=(ImageButton)findViewById(R.id.PauseImageButton);mNextImageButton=(ImageButton)findViewById(R.id.NextImageButton);/*初始化拖动条和当前进度显示值*/mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);curProgressText=(TextView)findViewById(R.id.currentProgress);curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);/*监听停止按钮*/mStopImageButton.setOnClickListener(newImageButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){/*判断是否正在播放歌曲*/if(mMediaPlayer.isPlaying()){/*如果在播放歌曲时,按下开始按钮,则重开开始播放*/mMediaPlayer.reset();}}});/*监听开始按钮*/mStartImageButton.setOnClickListener(newImageButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){/*播放当前选择歌曲,通过listView列表中onListItemClick方法得到选择的时第几项*/playMusic(MUSIC_PATH+mMusicList.get(currentListItme));/*开始播放歌曲时,同步进行更新拖动条进度*/startSeekBarUpdate();}});/*监听暂停按钮*/mPauseImageButton.setOnClickListener(newImageButton.OnClickListener(){publicvoidonClick(Viewview){if(mMediaPlayer.isPlaying()){/*如果有播放歌曲,暂停*/mMediaPlayer.pause();}else{/*如果没有播放歌曲,则开始播放*/mMediaPlayer.start();}}});/*监听下一首按钮*/mNextImageButton.setOnClickListener(newImageButton.OnClickListener(){@OverridepublicvoidonClick(Viewarg0){nextMusic();}});/*监听上一首按钮*/mFrontImageButton.setOnClickListener(newImageButton.OnClickListener(){@OverridepublicvoidonClick(Viewarg0){FrontMusic();}});/*监听拖动条*/mSeekBar.setOnSeekBarChangeListener(newOnSeekBarChangeListener(){publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){//TODOAuto-generatedmethodstub/*如果拖动进度发生改变,则显示当前进度值*/curProgressText.setText(当前进度:+progress);}@OverridepublicvoidonStartTrackingTouch(SeekBarseekBar){//TODOAuto-generatedmethodstubcurProgressText.setText(拖动中...);}@OverridepublicvoidonStopTrackingTouch(SeekBarseekBar){//TODOAuto-generatedmethodstubintdest=seekBar.getProgress();intmMax=mMediaPlayer.getDuration();intsMax=mSeekBar.getMax();mMediaPlayer.seekTo(mMax*dest/sMax);}});}/*按键处理时间,当按下返回按键时的处理方法*/publicbooleanonKeyDown(intkeyCode,KeyEventevent){if(keyCode==KeyEvent.KEYCODE_BACK){mMediaPlayer.stop();mMediaPlayer.release();this.finish();returntrue;}returnsuper.onKeyDown(keyCode,event);}/*ListView选择项监听方法,即当前鼠标在列表中选择的第几项*/protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){currentListItme=position;playMusic(MUSIC_PATH+mMusicList.get(position));}/*更新播放列表*/publicvoidmusicList(){/*从指定的路径中读取文件,并与播放列表关联*/Filehome=newFile(MUSIC_PATH);/*读取指定类型的文件,在本程序,指定播放类型为mp3*/if(home.listFiles(newMusicFilter()).length0){/*读取文件*/for(Filefile:home.listFiles(newMusicFilter())){mMusicList.add(file.getName());}/*播放文件与播放列表关联*/ArrayAdapterStringmusicList=newArrayAdapterString(Mp3Player.this,R.layout.musicitme,mMusicList);setListAdapter(musicList);}}/*播放指定路径中的音乐*/privatevoidplayMusic(Stringpath){try{/*重置多媒体*/mMediaPlayer.reset();/*读取mp3文件*/mMediaPlayer.setDataSource(path);/*准备播放*/mMediaPlayer.prepare();/*开始播放*/mMediaPlayer.start();/*监听播放是否完成*/mMediaPlayer.setOnCompletionListener(newOnCompletionListener(){publicvoidonCompletion(MediaPlayerarg0){/*播放完当前歌曲,自动播放下一首*/nextMusic();}});}catch(IOExceptione){}}/*播放下一首*/privatevoidnextMusic(){if(++currentListItme=mMusicList.size()){currentListItme=0;}else{playMusic(MUSIC_PATH+mMusicList.get(currentListItme));}}/*播放上一首歌曲*/privatevoidFrontMusic(){if(--currentListItme=0){currentListItme=0;}else{
本文标题:安卓音乐播放器实现代码
链接地址:https://www.777doc.com/doc-7208076 .html