《安卓音乐播放器实现代码.doc》由会员分享,可在线阅读,更多相关《安卓音乐播放器实现代码.doc(44页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date安卓音乐播放器实现代码安卓音乐播放器实现代码1. Mp3Player.java 文件package com.jacky.multimedia;import java.io.File;import java.io.FilenameFilter;import java.io.IOException;import java.util.ArrayList;import jav
2、a.util.List;import android.app.ListActivity;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.Bundle;import android.os.Handler;import android.view.KeyEvent;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ImageBu
3、tton;import android.widget.ListView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;public class Mp3Player extends ListActivity /* Called when the activity is first created. */* 定义5个图像按钮 */private ImageButton mFrontImageButton = null
4、;private ImageButton mStartImageButton = null;private ImageButton mPauseImageButton = null;private ImageButton mStopImageButton = null;private ImageButton mNextImageButton = null;/*定义进度handler,显示百分比进度*/Handler mPercentHandler = new Handler();/*定义拖动条*/private SeekBar mSeekBar=null;/*定义显示文本框*/private
5、TextView curProgressText=null;private TextView curtimeAndTotaltime=null;/* 定于一个多媒体对象*/public MediaPlayer mMediaPlayer = null;/*定于一个数据播放列表,用来存放从指定文件中搜索到的文件*/private List mMusicList = new ArrayList();/* 定义在播放列表中的当前选择项 */private int currentListItme = 0;/*定义要播放的文件夹路径*/private static final String MUSIC_P
6、ATH = new String(/mnt/sdcard/);/* Called when the activity is first created. */Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);/* 更新播放列表*/musicList();/*初始化多媒体对象*/mMediaPlayer = new MediaPlayer();/*初始化图像按钮*/mFrontImageButton = (I
7、mageButton) 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
8、(R.id.NextImageButton);/*初始化拖动条和当前进度显示值*/mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);curProgressText=(TextView)findViewById(R.id.currentProgress);curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);/*监听停止按钮*/mStopImageButton.setOnClickListener(new ImageButton.OnClickListener()Ove
9、rridepublic void onClick(View v)/*判断是否正在播放歌曲*/if (mMediaPlayer.isPlaying()/*如果在播放歌曲时,按下开始按钮,则重开开始播放*/mMediaPlayer.reset(););/*监听开始按钮*/mStartImageButton.setOnClickListener(new ImageButton.OnClickListener()Overridepublic void onClick(View v) /*播放当前选择歌曲,通过listView列表中onListItemClick方法得到选择的时第几项*/playMusi
10、c(MUSIC_PATH + mMusicList.get(currentListItme);/*开始播放歌曲时,同步进行更新拖动条进度*/startSeekBarUpdate(););/*监听暂停按钮*/mPauseImageButton.setOnClickListener(new ImageButton.OnClickListener()public void onClick(View view)if (mMediaPlayer.isPlaying()/*如果有播放歌曲,暂停*/mMediaPlayer.pause();else/*如果没有播放歌曲,则开始播放*/mMediaPlayer
11、.start(););/*监听下一首按钮*/mNextImageButton.setOnClickListener(new ImageButton.OnClickListener()Overridepublic void onClick(View arg0)nextMusic(););/*监听上一首按钮*/mFrontImageButton.setOnClickListener(new ImageButton.OnClickListener()Overridepublic void onClick(View arg0)FrontMusic(););/*监听拖动条*/mSeekBar.setOn
12、SeekBarChangeListener(new OnSeekBarChangeListener() public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) / TODO Auto-generated method stub/*如果拖动进度发生改变,则显示当前进度值*/curProgressText.setText(当前进度: +progress);Overridepublic void onStartTrackingTouch(SeekBar seekBar) / TODO Auto-gen
13、erated method stubcurProgressText.setText(拖动中.);Overridepublic void onStopTrackingTouch(SeekBar seekBar) / TODO Auto-generated method stubint dest = seekBar.getProgress();int mMax = mMediaPlayer.getDuration();int sMax = mSeekBar.getMax();mMediaPlayer.seekTo(mMax*dest/sMax););/*按键处理时间,当按下返回按键时的处理方法*/
14、public boolean onKeyDown(int keyCode, KeyEvent event)if ( keyCode = KeyEvent.KEYCODE_BACK)mMediaPlayer.stop();mMediaPlayer.release();this.finish();return true;return super.onKeyDown(keyCode, event);/*ListView选择项监听方法,即当前鼠标在列表中选择的第几项*/protected void onListItemClick(ListView l, View v, int position, lo
15、ng id)currentListItme = position;playMusic(MUSIC_PATH + mMusicList.get(position);/*更新播放列表*/public void musicList()/*从指定的路径中读取文件,并与播放列表关联*/File home = new File(MUSIC_PATH);/*读取指定类型的文件,在本程序,指定播放类型为mp3*/if (home.listFiles(new MusicFilter().length 0)/*读取文件*/for (File file : home.listFiles(new MusicFilte
16、r()mMusicList.add(file.getName();/*播放文件与播放列表关联*/ArrayAdapter musicList = new ArrayAdapter(Mp3Player.this,R.layout.musicitme, mMusicList);setListAdapter(musicList);/*播放指定路径中的音乐*/private void playMusic(String path)try/* 重置多媒体 */mMediaPlayer.reset();/*读取mp3文件*/mMediaPlayer.setDataSource(path);/*准备播放*/m
17、MediaPlayer.prepare();/*开始播放*/mMediaPlayer.start();/*监听播放是否完成*/mMediaPlayer.setOnCompletionListener(new OnCompletionListener()public void onCompletion(MediaPlayer arg0)/*播放完当前歌曲,自动播放下一首*/nextMusic(););catch (IOException e)/*播放下一首*/private void nextMusic()if (+currentListItme = mMusicList.size()curre
18、ntListItme = 0;elseplayMusic(MUSIC_PATH + mMusicList.get(currentListItme);/*播放上一首歌曲*/private void FrontMusic()if (-currentListItme = 0)currentListItme = 0;elseplayMusic(MUSIC_PATH + mMusicList.get(currentListItme);/*更新拖动条进度*/public void startSeekBarUpdate()mPercentHandler.post(start);Runnable start=
19、new Runnable()Overridepublic void run() / TODO Auto-generated method stubmPercentHandler.post(updatesb);/用一个handler更新SeekBar;Runnable updatesb =new Runnable()Overridepublic void run() / TODO Auto-generated method stubint position = mMediaPlayer.getCurrentPosition(); int mMax = mMediaPlayer.getDurati
20、on();int sMax = mSeekBar.getMax(); mSeekBar.setProgress(position*sMax/mMax);curtimeAndTotaltime.setText(当前播放时间: +position/1000+秒+n歌曲总时间: +mMax/1000+秒);mPercentHandler.postDelayed(updatesb, 1000);/每秒钟更新一次;/*播放文件选择类*/class MusicFilter implements FilenameFilterpublic boolean accept(File dir, String name)/*指定扩展名类型*/return (name.endsWith(.mp3);2.main.xls文件3.musictime.xls4.AndroidManifest.xls-