Android中的视频用到的有view.SurfaceHolder和view.SurfaceView,还有就是用到了MediaPlayer.OnBufferingUpdateListener和MediaPlayer.OnBufferingUpdateListener这里就是视频的主要的代码。
Java代码:主类
复制代码
main.xml文件
复制代码
注意:在onDestory方法中一定要销毁mediaplayer对象
Java代码:主类
- package com.example.testmediaplayer;
-
- import android.app.Activity;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnBufferingUpdateListener;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
-
- public class MainActivity extends Activity implements
-
- OnBufferingUpdateListener, OnCompletionListener,
- MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
- private static final String TAG = "Activity1";
- private int mVideoWidth;
- private int mVideoHeight;
- private MediaPlayer mMediaPlayer;
- private SurfaceView mPreview;
- private SurfaceHolder holder;
- private String path;
-
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.activity_main);
-
- mPreview = (SurfaceView) findViewById(R.id.surfaceView1);
-
- /* 得到SurfaceHolder对象 */
- holder = mPreview.getHolder();
-
- /* 设置回调函数 */
- holder.addCallback(this);
-
- /* 设置风格 */
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-
- }
-
- private void playVideo() {
- try {
- path = Environment.getExternalStorageDirectory().getPath()
- + "/pandora-19.mov";
-
- /* 构建MediaPlayer对象 */
- mMediaPlayer = new MediaPlayer();
-
- /* 设置媒体文件路径 */
- mMediaPlayer.setDataSource(path);
-
- /* 设置通过SurfaceView来显示画面 */
- mMediaPlayer.setDisplay(holder);
-
- /* 准备 */
- mMediaPlayer.prepare();
-
- /* 设置事件监听 */
- mMediaPlayer.setOnBufferingUpdateListener(this);
- mMediaPlayer.setOnCompletionListener(this);
- mMediaPlayer.setOnPreparedListener(this);
- mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
-
- } catch (Exception e) {
- Log.e(TAG, "error: " + e.getMessage(), e);
- }
- }
-
- public void onBufferingUpdate(MediaPlayer arg0, int percent) {
- Log.d(TAG, "onBufferingUpdate percent:" + percent);
- }
-
- public void onCompletion(MediaPlayer arg0) {
- Log.d(TAG, "onCompletion called");
- mMediaPlayer.reset();
- playVideo();
- }
-
- public void onPrepared(MediaPlayer mediaplayer) {
- Log.d(TAG, "onPrepared called");
- mVideoWidth = mMediaPlayer.getVideoWidth();
- mVideoHeight = mMediaPlayer.getVideoHeight();
-
- if (mVideoWidth != 0 && mVideoHeight != 0) {
-
- /* 设置视频的宽度和高度 */
- holder.setFixedSize(mVideoWidth, mVideoHeight);
-
- /* 开始播放 */
- mMediaPlayer.start();
- }
-
- }
-
- /* 更改时出发的事件 */
- public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
- Log.d(TAG, "surfaceChanged called");
-
- }
-
- /* 销毁 */
- public void surfaceDestroyed(SurfaceHolder surfaceholder) {
- Log.d(TAG, "surfaceDestroyed called");
- }
-
- /* 当SurfaceHolder创建时触发 */
- public void surfaceCreated(SurfaceHolder holder) {
- Log.d(TAG, "surfaceCreated called");
- playVideo();
- }
-
- /* 销毁 */
- @Override
- protected void onDestroy() {
- super.onDestroy();
-
- if (mMediaPlayer != null) {
- mMediaPlayer.release();
- mMediaPlayer = null;
- }
-
- }
- }
- <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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <SurfaceView
- android:id="@+id/surfaceView1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
-
- </RelativeLayout>