Android MediaPlayer播放音乐并实现进度条

提前工作,往sd卡里放音乐文件

1、布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="歌曲名:"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="blueflawer.mp3"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放"
        android:id="@+id/play_pause"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="重置"
        android:id="@+id/reset"/>
    <SeekBar 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekbar"/>
</LinearLayout>

2、activity类

package cn.csdn.activity;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MyPlayerActivity extends Activity {
    private Button play_pause, reset;
    private SeekBar seekbar;
    private boolean ifplay = false;
    private MediaPlayer player = null;
    private String musicName = "blueflawer.mp3";
    private boolean iffirst = false;
    private Timer mTimer;  
    private TimerTask mTimerTask; 
    private boolean isChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        player = new MediaPlayer();
        findViews();// 各组件
    }

    private void findViews() {
        play_pause = (Button) findViewById(R.id.play_pause);
        reset = (Button) findViewById(R.id.reset);
        play_pause.setOnClickListener(new MyClick());
        reset.setOnClickListener(new MyClick());

        seekbar = (SeekBar) findViewById(R.id.seekbar);
        seekbar.setOnSeekBarChangeListener(new MySeekbar());
    }

    class MyClick implements OnClickListener {
        public void onClick(View v) {
            File file = new File(Environment.getExternalStorageDirectory(),
                    musicName);
            // 判断有没有要播放的文件
            if (file.exists()) {
                switch (v.getId()) {
                case R.id.play_pause:
                    if (player != null && !ifplay) {
                        play_pause.setText("暂停");
                        if (!iffirst) {
                            player.reset();
                            try {
                                player.setDataSource(file.getAbsolutePath());
                                player.prepare();// 准备

                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (IllegalStateException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            seekbar.setMax(player.getDuration());//设置进度条
                            //----------定时器记录播放进度---------//  
                            mTimer = new Timer();  
                            mTimerTask = new TimerTask() {  
                                @Override  
                                public void run() {   
                                    if(isChanging==true) { 
                                        return;  
                                    }
                                    seekbar.setProgress(player.getCurrentPosition());
                                }  
                            }; 
                            mTimer.schedule(mTimerTask, 0, 10); 
                            iffirst=true;
                        }
                        player.start();// 开始
                        ifplay = true;
                    } else if (ifplay) {
                        play_pause.setText("继续");
                        player.pause();
                        ifplay = false;
                    }
                    break;
                case R.id.reset:
                    if (ifplay) {
                        player.seekTo(0);
                    } else {
                        player.reset();
                        try {
                            player.setDataSource(file.getAbsolutePath());
                            player.prepare();// 准备
                            player.start();// 开始
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                }
            }
        }
    }
    //进度条处理
    class MySeekbar implements OnSeekBarChangeListener {
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            isChanging=true;  
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            player.seekTo(seekbar.getProgress());
            isChanging=false;  
        }

    }
    //来电处理
    protected void onDestroy() {
        if(player != null){
            if(player.isPlaying()){
                player.stop();
            }
            player.release();
        }
        super.onDestroy();
    }

    protected void onPause() {
        if(player != null){
            if(player.isPlaying()){
                player.pause();
            }
        }
        super.onPause();
    }

    protected void onResume() {
        if(player != null){
            if(!player.isPlaying()){
                player.start();
            }
        }
        super.onResume();
    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。