使用MediaPlayer和SurfaceView播放视频

使用MediaPlayer播放视频的步骤

     1、创建MediaPlyer的对象,并让他加载指定的视频文件。

     2、在界面布局文件中定义SurfaceView组件,或在程序中创建SurfaceView组件。并为SurfaceView的SurfaceHolder添加Callback监听器。   

     3、调用MediaPlayer对象的setDisplay(Surfaceolder sh)将所播放的视频图像输出到指定的SurfaceView组件

     4、调用MediaPlayer对象的start()、stop()、和pause()方法控制视频的播放


实例:

 

<RelativeLayout 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=".MainActivity" >

  <SurfaceView
        android:id="@+id/surface1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <LinearLayout
        android:id="@+id/linaer1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
       >
       <Button android:id="@+id/start"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/start"
           android:layout_weight="1"/>
       <Button 
           android:id="@+id/pre"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/pre"
           android:layout_weight="1"/>
             <Button 
           android:id="@+id/stop"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/stop"
           android:layout_weight="1"/>
       
        
    </LinearLayout>
      

</RelativeLayout>

package com.android.xiong.surfaceviewtest;

import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private SurfaceView surface1;
	private Button start, stop, pre;
	private MediaPlayer mediaPlayer1;

	private int postion = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		surface1 = (SurfaceView) findViewById(R.id.surface1);
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);
		pre = (Button) findViewById(R.id.pre);
		mediaPlayer1 = new MediaPlayer();
		//设置播放时打开屏幕
		surface1.getHolder().setKeepScreenOn(true);
		surface1.getHolder().addCallback(new SurfaceViewLis());
		start.setOnClickListener(this);
		stop.setOnClickListener(this);
		pre.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start:
			try {
				play();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.pre:
			if (mediaPlayer1.isPlaying()) {
				mediaPlayer1.pause();
			} else {
				mediaPlayer1.start();
			}
			break;
		case R.id.stop:
			if (mediaPlayer1.isPlaying())
				mediaPlayer1.stop();
			break;
		default:
			break;
		}

	}

	public void play() throws IllegalArgumentException, SecurityException,
			IllegalStateException, IOException {
		mediaPlayer1.reset();
		mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mediaPlayer1.setDataSource("/mnt/sdcard/通话录音/1.mp4");
		// 把视频输出到SurfaceView上
		mediaPlayer1.setDisplay(surface1.getHolder());
		mediaPlayer1.prepare();
		mediaPlayer1.start();
	}

	private class SurfaceViewLis implements SurfaceHolder.Callback {

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

		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			if (postion == 0) {
				try {
					play();
					mediaPlayer1.seekTo(postion);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {

		}

	}

	@Override
	protected void onPause() {
		if (mediaPlayer1.isPlaying()) {
			// 保存当前播放的位置
			postion = mediaPlayer1.getCurrentPosition();
			mediaPlayer1.stop();
		}
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if (mediaPlayer1.isPlaying())
			mediaPlayer1.stop();
		mediaPlayer1.release();
		super.onDestroy();
	}

}



使用MediaPlayer和SurfaceView播放视频

上一篇:SharePoint 2013 托管导航及相关配置 <二>


下一篇:Strom:pluggable scheduler 自己写的自定义分配任务