android中使用MediaPlayer和SurfaceView播放视频

package com.test.video;

import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {

    private MediaPlayer player;
    private SurfaceView view ;
    private int position = 0;
    private ImageButton paly, pause, stop;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        view = (SurfaceView) findViewById(R.id.surfaceView);
        player = new  MediaPlayer();
        paly = (ImageButton) findViewById(R.id.play);
        pause = (ImageButton) findViewById(R.id.pause);
        stop = (ImageButton) findViewById(R.id.stop);
        // 设置SurfaceView自己不管理缓冲区
        view.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // 设置播放时打开屏幕
        view.setKeepScreenOn(true);
        view.getHolder().addCallback(new Surfacelistener());
        
        paly.setOnClickListener(this); 
        pause.setOnClickListener(this);  
        stop.setOnClickListener(this); 
    }


    @Override
    public void onClick(View v) {
     
        try {
            switch (v.getId()) {
            // 单击播放按钮
            case R.id.play:
                 play();
                break;
            // 暂停按钮
            case R.id.pause:
                 if (player.isPlaying()) {
                     player.pause();
                     position = player.getCurrentPosition();
                 } else {
                     if (position > 0) player.seekTo(position);
                     player.start();
                 }
                break;
            // 停止按钮
            case R.id.stop:
                 if (player.isPlaying()) {
                     player.stop();
                 }
                break;

            default:
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void play() throws Exception {
        player.reset();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        // 设置需要播放的视频
        Uri uri =  Uri.parse("android.resource://com.test.video/" + R.raw.video );
        player.setDataSource(this, uri);
        // 把视频画面输出到SurfaceView
        player.setDisplay(view.getHolder());
        player.prepare();
        player.start();
    }

    private  class Surfacelistener implements Callback {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
             if (position > 0) {
                 try {
                     // 开始播放
                     play();
                     // 并直接从指定位置开始播放
                     player.seekTo(position);
                     position = 0;
                 } catch (Exception e ) {
                     
                 }
             }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {}

    }

    // 当其他的Activity被打开时,暂停播放
    @Override
    protected void onPause() {
        if (player.isPlaying()) {
            position = player.getCurrentPosition();
            // 暂停播放
            player.stop();
        }
        super.onPause();
    }
    
    @Override
    protected void onDestroy() {
        if (player.isPlaying()) {
            player.stop();
        }
        player.release();
        super.onDestroy();
    }
}

android中使用MediaPlayer和SurfaceView播放视频,,5-wow.com

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