最近项目中遇到一点小需求,就是有个本地音乐播放 ,功能简单,实现了 播放、暂停、下一首和上一首,有基本需要的小伙伴可以参考(其他篇章写过遍历文件)
1.首先是xml 布局
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
android:layout_height="120dp"
android:layout_alignParentBottom="true">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:progress="0" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/lv_seek_bar"
android:text="00:00" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00/"
android:layout_below="@+id/lv_seek_bar"
android:layout_toLeftOf="@+id/tv_time_local"
android:layout_toStartOf="@+id/tv_time_local" />
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/lv_seek_bar"
android:text="歌曲名称"
android:singleLine="true"/>
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#669fff"
android:orientation="horizontal">
android:layout_height="60dp"
android:src="@drawable/music" />
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:background="@drawable/previous" />
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="@drawable/play" />
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="@drawable/next" />
2 .其次就是代码了(因为我们的不是全部使用手点击操作,所以只附上点击操作的代码)
public class MusicActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MusicActivity";
public static MusicActivity instance;
private MediaPlayer player;
private String musicName;
private ImageButton ibtn_play, ibtn_next, ibtn_previous;
private TextView tv_time_local, tv_musicName, tv_time_current;
private SeekBar seek_bar;
private ListView lv_local_music;
private List
private MusicListAdapter musicListAdapter;
private String speakText = "";
private SimpleDateFormat format = new SimpleDateFormat("mm:ss");
public static final int MUSIC_DURATION = 0X324;
public static final int MUSIC_POSITON = 0X325;
private int pPosition;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MUSIC_DURATION:
if (player != null) {
seek_bar.setMax(player.getDuration() / 1000);
String time = format.format(new Date(player.getDuration()));
tv_time_local.setText(time);
}
break;
case MUSIC_POSITON:
if (player != null) {
try {
seek_bar.setProgress(player.getCurrentPosition() / 1000);
tv_time_current.setText(format.format(new Date(player.getCurrentPosition())) + "/");
handler.sendEmptyMessageDelayed(MUSIC_POSITON, 1000);
} catch (Exception e) {
e.getStackTrace();
}
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_local);
EventBus.getDefault().register(this);
SpeechApp.addActivity(this);
instance = this;
SpeechApp.scene.add("91", "music", true, "1", true);
initview();
mMusicList = (List
musicListAdapter = new MusicListAdapter(this, mMusicList);
lv_local_music.setAdapter(musicListAdapter);
lv_local_music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
ibtn_play.setBackgroundResource(R.drawable.play); //更改按键状态图标
pPosition = position;
String musicPath = mMusicList.get(pPosition).getPath();
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
Log.i(TAG, "------musicPath------>" + musicPath);
startMusic(musicPath);
}
});
//进度条监听
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
player.seekTo(progress * 1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
/**
* 初始化Ui
*/
private void initview() {
ibtn_play = (ImageButton) findViewById(R.id.ibtn_play);
ibtn_next = (ImageButton) findViewById(R.id.ibtn_next);
ibtn_previous = (ImageButton) findViewById(R.id.ibtn_previous);
lv_local_music = (ListView) findViewById(R.id.lv_local_music);
tv_time_local = (TextView) findViewById(R.id.tv_time_local);
tv_musicName = (TextView) findViewById(R.id.tv_musicName);
tv_time_current = (TextView) findViewById(R.id.tv_time_current);
seek_bar = (SeekBar) findViewById(R.id.lv_seek_bar);
ibtn_play.setOnClickListener(this);
ibtn_next.setOnClickListener(this);
ibtn_previous.setOnClickListener(this);
//创建mediaplayer
player = new MediaPlayer();
}
int totalNum;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ibtn_play:
if (player.isPlaying()) { //如果正在播放则暂停
player.pause();
ibtn_play.setBackgroundResource(
R.drawable.pause); //更改按键状态图标
} else { //如果没有播放则恢复播放
player.start();
ibtn_play.setBackgroundResource(
R.drawable.play); //更改按键状态图标
}
break;
case R.id.ibtn_next:
ibtn_play.setBackgroundResource(R.drawable.play); //更改按键状态图标
totalNum = mMusicList.size(); //获得音乐的数目
if (pPosition == totalNum - 1) { //如果已经是最后一首,则播放第一首
pPosition = 0;
} else {
pPosition += 1; //否则播放下一首
}
String nextMusicPath = mMusicList.get(pPosition).getPath();//得到将要播放的音乐路径
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
startMusic(nextMusicPath); //开始播放
break;
case R.id.ibtn_previous:
ibtn_play.setBackgroundResource(R.drawable.play); //更改按键状态图标
totalNum = mMusicList.size(); //获得音乐的数目
if (pPosition == 0) { //如果已经时第一首则播放最后一首
pPosition = totalNum - 1;
} else { //否则播放上一首
pPosition -= 1;
}
String previousMusicPath = mMusicList.get(pPosition).getPath();//得到将要播放的音乐路径
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
startMusic(previousMusicPath); //开始播放
break;
}
}
/**
* 播放音乐
*/
private void startMusic(String musicPath) {
//重置,第一次不需要重置
player.reset();
if (player.isPlaying()) {
player.release();
}
//设置播放的文件的路径
try {
player.setDataSource(musicPath);
Log.i("tag", "-------------" + musicName);
//设置准备完成的监听器
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//播放
mp.start();
handler.sendEmptyMessage(MUSIC_DURATION);
handler.sendEmptyMessage(MUSIC_POSITON);
SpeechApp.speechHelper.nowIsAwaken(false);
}
});
//异步加载
player.prepareAsync();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
int num = mMusicList.size(); //获得音乐的数目
if (pPosition == num - 1) { //如果已经是最后一首,则播放第一首
pPosition = 0;
} else {
pPosition += 1; //否则播放下一首
}
String musicPath = mMusicList.get(pPosition).getPath();//得到将要播放的音乐路径
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
startMusic(musicPath); //开始播放
}
});
} catch (Exception e) {
e.printStackTrace();
}
return;
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
if (player.isPlaying()) {
player.release();
}
}
}
希望对大家有所帮助~