Android 实现简单媒体播放器功能

一、概述:

因为播放视频需要很大的内存,所以必须使用surfaceview ,
surfaceview 里实现了双缓冲的功能,

———————————————————————
有需求者请加qq:136137465,非诚勿扰
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
——————————————————————–

二、全部代码:

/**
 * @描述         使用surfaceview 创建视频
 * @项目名称      App_Basic
 * @包名         com.example.basic.media
 * @类名         MediaPlayerActivity
 * @author      chenlin
 * @date        2013-3-25 上午7:36:32
 * @version     1.0
 */

public class MediaPlayerActivity extends BaseActivity {
    private MediaPlayer mPlayer;
    private Button mBtnPlay;
    private Button mBtnPause;
    private SurfaceView mSurfaceView;
    private int mCurrentPosition;
    private SeekBar seekBar;
    private boolean isPlaying;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSurfaceView = new SurfaceView(this);
        seekBar = new SeekBar(this);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                if (mPlayer != null && isPlaying) {
                    mPlayer.seekTo(progress);
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // TODO Auto-generated method stub
            }
        });
        mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceView.getHolder().addCallback(new Callback() {


            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                if (mPlayer!= null && mPlayer.isPlaying()) {
                    mCurrentPosition = mPlayer.getCurrentPosition();
                    stop();
                }

            }
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (mCurrentPosition > 0) {
                    play(mCurrentPosition);
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                // TODO Auto-generated method stub
            }
        });
    }

    public void play(final int currentPosition){
        try {
            mPlayer  = new MediaPlayer();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            Uri myUri = Uri.fromFile(new File("sdcard/mp3/1.mp3"));
            mPlayer.setDataSource(getApplicationContext(), myUri);
            mPlayer.setLooping(true);
            mPlayer.setDisplay(mSurfaceView.getHolder());
            mPlayer.prepareAsync();
            //mPlayer.prepare();
            mBtnPlay.setEnabled(false);
            mPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mBtnPlay.setEnabled(true);
                    mPlayer.release();
                }
            });

            mPlayer.setOnErrorListener(new OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mBtnPlay.setEnabled(true);
                    mPlayer.release();
                    isPlaying = false;
                    return false;
                }
            });

            mPlayer.setOnPreparedListener(new OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {
                    mPlayer.start();
                    mPlayer.seekTo(currentPosition);
                    seekBar.setMax(mPlayer.getDuration());
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            isPlaying = true;
                            while(isPlaying){
                                seekBar.setProgress(mPlayer.getCurrentPosition());
                                SystemClock.sleep(500);
                            }
                        }
                    });
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pause(){
        if ("接续".equals(mBtnPause.getText().toString())) {
            mBtnPause.setText("暂停");
            mPlayer.start();
            return;
        }
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.pause();
            mBtnPause.setText("继续");
            return;
        }
    }

    public void reset(){
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.seekTo(0);
            return;
        }
        play(0);
    }

    public void stop(){
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
            mBtnPlay.setEnabled(true);
            isPlaying = false;
        }
    }
}

你可能感兴趣的:(android,多媒体,android,基础)