MediaPlayer+SeekBar 上一曲下一曲

来源:互联网 发布:免费手机视频剪辑软件 编辑:程序博客网 时间:2024/05/17 05:13

主方法里的代码

package com.example.day12mediaplayerhome;


import java.io.IOException;


import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnClickListener {
MediaPlayer mp;
SeekBar sb;
ListView lv;
Button btn_pro, btn_start_pause, btn_next;
String[] songNames = { "bajie.mp3", "ruguoyoulaisheng.mp3",
"xiyangyang.mp3" };
int index = 0;
//消息机制,让seekbar可以和音乐播放同步显示进度
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(mp != null){
//使用mp当前播放的音乐总长度来设置seekbar的最大值
sb.setMax(mp.getDuration());
//使用mp当前播放的音乐进度来设置seekbar当前显示进度值
sb.setProgress(mp.getCurrentPosition());
//重复发送更新的消息
sendEmptyMessageDelayed(0, 500);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// mp = new MediaPlayer();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,songNames);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
index = position;
stopMusic();
playMusic(position);
}
});
//seekbar的监听方法  
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//当手指抬起时   开始播放音乐
startMusic();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//如果是用户操作  则让音乐先暂停  然后把进度条的进度值 赋值给mp的音乐播放进度
if(fromUser){
if(mp != null){
pauseMusic();
//设置当前音乐的播放进度
mp.seekTo(progress);
}
}
}
});
}
/**
* 创建一个播放音乐的方法
* @param position
*/
protected void playMusic(int position) {
btn_start_pause.setText("暂停");
// Toast.makeText(MainActivity.this, "" + position, 0).show();
// MediaPlayer.create(MainActivity.this, R.id.action_settings);
//从assets文件夹中获取资源
AssetFileDescriptor afd;
try {
afd = getResources().getAssets().openFd(songNames[position]);
if (afd != null){
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepareAsync();
//当mp开始播放音乐的时候   启动消息完成seekbar的同步显示进度
handler.sendEmptyMessage(0);
//设置准备完成的方法
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});

//mp的监听方法    监听当前音乐是否播放完成
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//当播放完成被监听到的时候  调用播放下一首的方法
nextMusic();
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void pauseMusic(){
if(mp != null && mp.isPlaying()){
mp.pause();
}
}
public void startMusic(){
if(mp != null){
mp.start();
}
}
/**
* 停止当前播放的音乐
*/
public void stopMusic(){
if(mp != null){
//停止当前音乐的时候  要停止消息的发送
handler.removeMessages(0);
sb.setProgress(0);
mp.stop();
mp.release();
mp = null;
}
}
/**
* 初始化控件
*/
private void initView() {
sb = (SeekBar) findViewById(R.id.sb);
lv = (ListView) findViewById(R.id.lv);
btn_pro = (Button) findViewById(R.id.btn_pro);
btn_start_pause = (Button) findViewById(R.id.btn_start_pause);
btn_next = (Button) findViewById(R.id.btn_next);
btn_pro.setOnClickListener(this);
btn_start_pause.setOnClickListener(this);
btn_next.setOnClickListener(this);
}
/**
* 按钮的监听方法
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_pro:
proMusic();
break;
case R.id.btn_start_pause:
//mp如果不等于空 并且  mp处于播放状态  则暂停 否在 播放
if(mp != null ){
if(mp.isPlaying()){
pauseMusic();
btn_start_pause.setText("播放");
}else{
startMusic();
btn_start_pause.setText("暂停");
}
}
break;
case R.id.btn_next:
nextMusic();
break;


default:
break;
}
}
private void nextMusic() {
stopMusic();
if(index < songNames.length - 1){
index++;
}
playMusic(index);
}
private void proMusic() {
stopMusic();
if(index > 0){
index--;
}
playMusic(index);
}
}


布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:text="八维音乐" 
        android:textColor="#f00"
        />
    <ListView 
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tv"
        android:layout_above="@+id/sb"
        >
        
    </ListView>
    <SeekBar 
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll"
        />
    <LinearLayout 
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
        <Button 
            android:id="@+id/btn_pro"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="上一曲"
            />
        <Button 
            android:id="@+id/btn_start_pause"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="播放"
            />
        <Button 
            android:id="@+id/btn_next"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="下一曲"
            />
    </LinearLayout>


</RelativeLayout>