main.xml布局文件添加用于视频画面绘制的SurfaceView控件:

<SurfaceViewandroid:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />

SurfaceViewsurfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);

surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率

/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/

surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

MediaPlayermediaPlayer = new MediaPlayer();

mediaPlayer.reset();//重置为初始状态

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

/* 设置Video影片以SurfaceHolder播放 */

mediaPlayer.setDisplay(surfaceView.getHolder());

mediaPlayer.setDataSource("/mnt/sdcard/oppo.mp4");

mediaPlayer.prepare();//缓冲

mediaPlayer.start();//播放

mediaPlayer.pause();//暂停播放

mediaPlayer.start();//恢复播放

mediaPlayer.stop();//停止播放

mediaPlayer.release();//释放资源

package cn.itcast.video;

import java.io.IOException;

import android.app.Activity;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.util.Log;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageButton;

public class VideoActivity extends Activity {

private static final String TAG = "VideoActivity";

private EditTextfilenameText;

private SurfaceViewsurfaceView;

private MediaPlayermediaPlayer;

private String filename;//当前播放文件的名称

private int position;//记录播放位置

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.mediaPlayer = new MediaPlayer();

this.filenameText = (EditText) this.findViewById(R.id.filename);

this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);

ImageButtonplayButton = (ImageButton) this.findViewById(R.id.play);

ImageButtonpauseButton = (ImageButton) this.findViewById(R.id.pause);

ImageButtonresetButton = (ImageButton) this.findViewById(R.id.reset);

ImageButtonstopButton = (ImageButton) this.findViewById(R.id.stop);

ButtonClickListener listener = new ButtonClickListener();

playButton.setOnClickListener(listener);

pauseButton.setOnClickListener(listener);

resetButton.setOnClickListener(listener);

stopButton.setOnClickListener(listener);

/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/

this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

this.surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率

this.surfaceView.getHolder().setKeepScreenOn(true);

this.surfaceView.getHolder().addCallback(new SurfaceListener());

}

private class ButtonClickListener implements View.OnClickListener{

@Override

public void onClick(View v) {

try {

switch (v.getId()) {

case R.id.play://来自播放按钮

filename = filenameText.getText().toString();

play();

break;

case R.id.pause://来自暂停按钮

if(mediaPlayer.isPlaying()){

mediaPlayer.pause();

}else{

mediaPlayer.start();

}

break;

case R.id.reset://来自重新播放按钮

if(!mediaPlayer.isPlaying()) play();

mediaPlayer.seekTo(0);

break;

case R.id.stop://来自停止按钮

if(mediaPlayer.isPlaying()) mediaPlayer.stop();

break;

}

} catch (Exception e) {

Log.e(TAG, e.toString());

}

}

}

/**

* 播放视频

*/

private void play() throws IOException {

mediaPlayer.reset();

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mediaPlayer.setDataSource("/mnt/sdcard/"+ filename);//设置需要播放的视频

mediaPlayer.setDisplay(surfaceView.getHolder());//把视频画面输出到SurfaceView

mediaPlayer.prepare();

mediaPlayer.start();

}

private class SurfaceListener implements SurfaceHolder.Callback{

@Override

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

}

@Override

public void surfaceCreated(SurfaceHolder holder) {//方法在onResume()后被调用

Log.i(TAG, "surfaceCreated()");

if(position>0 && filename!=null){

try {

play();

mediaPlayer.seekTo(position);

position = 0;

} catch (Exception e) {

Log.e(TAG, e.toString());

}

}

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

Log.i(TAG, "surfaceDestroyed()");

}

}

@Override

protected void onPause() {//当其他Activity被打开,停止播放

if(mediaPlayer.isPlaying()){

position = mediaPlayer.getCurrentPosition();//得到播放位置

mediaPlayer.stop();

}

super.onPause();

}

@Override

protected void onDestroy() {

if(mediaPlayer.isPlaying()) mediaPlayer.stop();

mediaPlayer.release();

super.onDestroy();

}

}

<?xml version="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:background="#FFFFFF"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/filename"

/>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="oppo.mp4"

android:id="@+id/filename"

/>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/play"

android:id="@+id/play"

/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/pause"

android:id="@+id/pause"

/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/reset"

android:id="@+id/reset"

/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/stop"

android:id="@+id/stop"

/>

</LinearLayout>

<SurfaceView

android:layout_width="fill_parent"

android:layout_height="240dip"

android:id="@+id/surfaceView"

/>

</LinearLayout>

更多相关文章

  1. Android:Eclipse如何设置格式化Java代码
  2. Android(安卓)VideoView通过Intent.ACTION_VIEW播放视频(4)
  3. 【明哥版】2020最新Android(安卓)Studio Win10 安装教程
  4. Android新手入门1
  5. Android开发项目--跑腿APP-跑儿
  6. Android仿微信下拉列表实现(附源码)
  7. 基于ffmpeg+opengl+opensl es的android视频播放器
  8. android 9 patch
  9. 第2步:第一个“Hello,world!”之Android(安卓)App(从零开始学Andro

随机推荐

  1. Android(安卓)权限控制代码分析
  2. Android应用程序启动
  3. Android(安卓)初始化语言(Android(安卓)in
  4. android imeOptions
  5. android调色器的实现
  6. app测试教程:ios与android的区别
  7. app测试教程:ios与android的区别
  8. Android(安卓)学习笔记-布局2
  9. android字符串资源字符format
  10. Android(安卓)-- Looper.prepare()和Loop