Android几种播放视频的方式

1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。

2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。

3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活。


1、调用其自带的播放器:

        Uri uri = Uri.parse("file:///sdcard/DCIM/Camera//VID_Test.mp4");
        //调用系统自带的播放器
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Log.e("tag", uri.toString());
        intent.setDataAndType(uri, "video/mp4");
        startActivity(intent);

2、使用VideoView来实现:

        Uri uri2 = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/VID_Test.mp4");
        Log.e("tag","url2:"+uri2.toString());
        Uri uri = Uri.parse("file:///sdcard/DCIM/Camera//VID_Test.mp4");
        Log.e("tag","url2:"+uri.toString());
        VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
        videoView.setMediaController(new MediaController(this));
        videoView.setVideoURI(uri);
        videoView.start();
        videoView.requestFocus();

第一个路径和第二个路径都是一样的。


3、使用SurfaceView:

(1)activity:

package com.zhangli.videotest;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {
    /** Called when the activity is first created. */
    MediaPlayer player;
    SurfaceView surface;
    SurfaceHolder surfaceHolder;
    Button play,pause;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.surface_layout);
        play=(Button)findViewById(R.id.button1);
        pause=(Button)findViewById(R.id.button2);
        surface=(SurfaceView)findViewById(R.id.surfaceView);

        surfaceHolder=surface.getHolder();//SurfaceHolder是SurfaceView的控制接口
        surfaceHolder.addCallback(this);//因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
        surfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型

        play.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                player.start();
            }});
        pause.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                player.pause();
            }});
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
        player=new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDisplay(surfaceHolder);
        //设置显示视频显示在SurfaceView上
        try {
            player.setDataSource("/sdcard/DCIM/Camera/VID_Test.mp4");
            player.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(player.isPlaying()){
            player.stop();
        }
        player.release();
        //Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
    }
}

布局:



    

    
    

    



(2)activity:

package com.zhangli.videotest;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;

public class Main2Activity extends AppCompatActivity {
    /** Called when the activity is first created. */
    private EditText filenamEditText;
    private MediaPlayer mediaPlayer;
    private String filename;
    private SurfaceView surfaceView;
    private int prosition=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        filenamEditText=(EditText) this.findViewById(R.id.filename);
        surfaceView=(SurfaceView)this.findViewById(R.id.surfaceview);
        surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
        surfaceView.getHolder().addCallback(new SurceCallBack());//对surface对象的状态进行监听
        mediaPlayer=new MediaPlayer();

        ButtonOnClikListiner buttonOnClikListinero=new ButtonOnClikListiner();
        Button start=(Button) this.findViewById(R.id.play);
        Button pause=(Button) this.findViewById(R.id.pause);
        Button stop=(Button) this.findViewById(R.id.stop);
        Button replay=(Button) this.findViewById(R.id.reset);
        start.setOnClickListener(buttonOnClikListinero);
        pause.setOnClickListener(buttonOnClikListinero);
        stop.setOnClickListener(buttonOnClikListinero);
        replay.setOnClickListener(buttonOnClikListinero);
    }

    private final class ButtonOnClikListiner implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(Environment.getExternalStorageState()==Environment.MEDIA_UNMOUNTED){
                Toast.makeText(Main2Activity.this, "sd卡不存在", Toast.LENGTH_SHORT).show();
                return;
            }
            filename=filenamEditText.getText().toString();
            filename="/DCIM/Camera//"+filename;

            switch (v.getId()) {
                case R.id.play:
                    play();
                    break;
                case R.id.pause:
                    if(mediaPlayer.isPlaying()){
                        mediaPlayer.pause();
                    }else{
                        mediaPlayer.start();
                    }
                    break;
                case R.id.reset:
                    if(mediaPlayer.isPlaying()){
                        mediaPlayer.seekTo(0);
                    }else{
                        play();
                    }
                    break;
                case R.id.stop:
                    if(mediaPlayer.isPlaying()){
                        mediaPlayer.stop();
                    }
                    break;
            }
        }
    }

    private void play() {
        try {
            File file=new File(Environment.getExternalStorageDirectory(),filename);
            Log.e("tag","file:"+file);
            mediaPlayer.reset();//重置为初始状态
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置音乐流的类型
            mediaPlayer.setDisplay(surfaceView.getHolder());//设置video影片以surfaceviewholder播放
            mediaPlayer.setDataSource(file.getAbsolutePath());//设置路径
            mediaPlayer.prepare();//缓冲
            mediaPlayer.start();//播放
        } catch (Exception e) {
            Log.e("tag", e.toString());
            e.printStackTrace();
        }
    }

    private final class SurceCallBack implements SurfaceHolder.Callback{
        /**
         * 画面修改
         */
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {
            // TODO Auto-generated method stub

        }
        /**
         * 画面创建
         */
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if(prosition>0&&filename!=null){
                play();
                mediaPlayer.seekTo(prosition);
                prosition=0;
            }
        }

        /**
         * 画面销毁
         */
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if(mediaPlayer.isPlaying()){
                prosition=mediaPlayer.getCurrentPosition();
                mediaPlayer.stop();
            }
        }
    }
}

布局:




    

    
    

        

不清楚的话,将demo下下来看。

http://download.csdn.net/detail/zhangli_/9460243



你可能感兴趣的:(Android几种播放视频的方式)