Android播放视频三种方式

需要在sdcard中放入一个视频文件

1.调用系统自带的播放器

Intent intent=new Intent(Intent.ACTION_VIEW);
    		Uri uri=Uri.parse("/sdcard/a.mp4");
    		intent.setDataAndType(uri, "video/mp4");
        	startActivity(intent);

2.利用VideoPlayer

布局文件如下:

<LinearLayout 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=".VideoViewActivity"
    android:orientation="vertical" >

      <VideoView
        android:id="@+id/videoview"
        android:layout_width="320dp"
        android:layout_height="220dp"
        android:layout_gravity="center"
       >
    </VideoView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/video_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放"
            android:onClick="onBtnClick"
             >
        </Button>
        <Button
            android:id="@+id/video_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onBtnClick"
            android:text="暂停" >
        </Button>

        <Button
            android:id="@+id/video_stop"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onBtnClick"
            android:text="停止" >
        </Button>
    </LinearLayout>

</LinearLayout>

Activity文件如下:

package com.example.videoplayer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewActivity extends Activity {

	private VideoView videoView;
	private MediaController controller;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_view);
		controller=new MediaController(VideoViewActivity.this);
		videoView=(VideoView) findViewById(R.id.videoview);
		videoView.setVideoPath("/sdcard/a.mp4");
		videoView.setMediaController(controller);
		videoView.requestFocus();
	}
	public void onBtnClick(View view){
		
		if(view.getId()==R.id.video_play){
			videoView.start();
		}
		if(view.getId()==R.id.video_pause){
			videoView.pause();
		}
		if(view.getId()==R.id.video_stop){
			videoView.stopPlayback();
		}
		
		
		
	}
	

}

第三种方式:SurfaceView

Activity代码如下:

package com.example.videoplayer;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements SurfaceHolder.Callback{
	MediaPlayer player;
    SurfaceView surface;
    SurfaceHolder surfaceHolder;
    Button play,pause,stop;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play=(Button)findViewById(R.id.button1);
        pause=(Button)findViewById(R.id.button2);
        stop=(Button)findViewById(R.id.button3);
        surface=(SurfaceView)findViewById(R.id.surface);
        
        surfaceHolder=surface.getHolder(); //SurfaceHolder是SurfaceView的控制接口
        surfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
        surfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认   
        //SURFACE_TYPE_PUSH_BUFFERS:标识数据来源于其他对象,比如照相机,比如视频播放服务器
        //(android内部有视频播放的服务器,所有播放视频相当于客户端)
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型
 
        play.setOnClickListener(new OnClickListener(){
             @Override
            public void onClick(View v) {
                player.start();
            }});

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

        stop.setOnClickListener(new OnClickListener(){
             @Override
            public void onClick(View v) {
                player.stop();
            }});
    }
 
    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }
 
    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
    	//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
    	System.out.println("--------------------");
        player=new MediaPlayer();
        //必须要有这个方法
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDisplay(surfaceHolder);
        //设置显示视频显示在SurfaceView上
         try {
                player.setDataSource("/sdcard/a.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销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
    }
}




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