(黎活明老师讲学)Android学习(四)---视频播放器

在手机上播放一个视频文件,我们需要用到一个控件。

SurfaceView---这个控件是显示一个视频区域,用来播放视频的。

先看看布局:

(黎活明老师讲学)Android学习(四)---视频播放器_第1张图片

布局的代码很简单:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/filename" />
    <EditText
        android:id="@+id/filename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:text="oppo.mp4"
        android:hint="@string/hello_world" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/play"
            android:layout_weight="1"
            android:contentDescription="@string/hello_world" />
        <ImageButton
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pause"
            android:contentDescription="@string/hello_world"
            android:layout_weight="1" />
        <ImageButton
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/reset"
            android:contentDescription="@string/hello_world"
            android:layout_weight="1" />
        <ImageButton
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/hello_world"
            android:src="@drawable/stop"
            android:layout_weight="1" />
    </LinearLayout>

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="240dip"
        android:id="@+id/surfaceView" />
    
</LinearLayout>

在看看实现的代码:

package com.itcast.videoplay;

import java.io.File;
import java.io.IOException;

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

public class MainActivity extends Activity implements OnClickListener{

	private EditText filenameText;
	private SurfaceView surfaceView;
	private ImageButton playButton, pausebutton, resetbutton, stopButton;
	
	private MediaPlayer mediaPlayer;
	
	private String filename;
	private int position;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initLayout();
	}

	private void initLayout(){
		
		filenameText = (EditText) findViewById(R.id.filename);
		
		surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
		surfaceView.getHolder().setFixedSize(176, 144);  //设置分辨率
		//下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
		surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceView.getHolder().addCallback(new SurfaceCallback());
		
		playButton = (ImageButton) findViewById(R.id.play);
		playButton.setOnClickListener(this);
		pausebutton = (ImageButton) findViewById(R.id.pause);
		pausebutton.setOnClickListener(this);
		resetbutton = (ImageButton) findViewById(R.id.reset);
		resetbutton.setOnClickListener(this);
		stopButton = (ImageButton) findViewById(R.id.stop);
		stopButton.setOnClickListener(this);
		
		mediaPlayer = new MediaPlayer();
	}

	@Override
	public void onClick(View v) {
		
		filename = filenameText.getText().toString();
		
		try {
			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;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	
	private void play() throws Exception{
		File file = new File(getCacheDir(), filename);
		mediaPlayer.reset();//重置为初始状态
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		//设置Video影片以SurfaceHolder播放 
		mediaPlayer.setDisplay(surfaceView.getHolder());
		mediaPlayer.setDataSource(file.getAbsolutePath());
		mediaPlayer.prepare();//缓冲				
		mediaPlayer.start();//播放
	}
	
	/**
	 * 比如一个电话打进来了,视频播放就得暂停,等待通话结束,继续播放
	 * @author TF
	 *
	 */
	private class SurfaceCallback implements Callback{

		//重新创建一个Activity,把保存的位置提取出来,继续播放
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			if(position > 0 && filename != null){
				try {
					play();
					mediaPlayer.seekTo(position);
					position = 0;
				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

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

		//销毁当前正在播放的视频,记录下当前播放的位置
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			if(mediaPlayer.isPlaying()){
				position = mediaPlayer.getCurrentPosition();
				mediaPlayer.stop();
			}
		}
		
	}
}
注释已经写的很详细了,主要用到的还是Mediaplayer这个流媒体播放类。有的模拟器可能播放不了,你可以尝试一下却换到Android 2.0版本的模拟器试试...

你可能感兴趣的:(android,视频,视频播放器,mediaplayer)