android控件之VideoView建立自己的播放器

简介

  用来播放视频文件。该VideoView类可以加载各种来源的图像(如资源或内容提供商),需要计算它从视频测量,以便它可以在任何布局管理器使用,并提供诸如缩放和着色的各种显示选项。在其他的平台上面可能VideoPlayer开发是一个比较有挑战性的工作,但是在Android上面VideoPlayer的开发,基本上可以做到傻瓜式啦。本文简单对VideoPlayer的开发进行简单的介绍。

  实例

  1.布局文件

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

  < LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content">

  < VideoView android:id="@+id/videoview"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:layout_centerInParent="true"

  />

  < /LinearLayout>

  2.Java代码

  package com.wjq;

  import android.app.Activity;

  import android.content.pm.ActivityInfo;

  import android.media.MediaPlayer;

  import android.NET.Uri;

  import android.os.Bundle;

  import android.os.Environment;

  import android.util.Log;

  import android.widget.MediaController;

  import android.widget.VideoView;

  public class VideoViewDemo extends Activity implements MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener{

  public static final String TAG = "VideoPlayer";

  private VideoView mVideoView;

  private Uri mUri;

  private int mPositionWhenPaused = -1;

  private MediaController mMediaController;

  /* (non-Javadoc)

  * @see android.app.Activity#onCreate(android.os.Bundle)

  */

  @Override

protected void onCreate(Bundle savedInstanceState) {

  // TODO Auto-generated method stub

  super.onCreate(savedInstanceState);

  setContentView(R.layout.videoview);

  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

  mVideoView = (VideoView)findViewById(R.id.videoview);

  //文件路径

  mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/ziranyouli.3gp");

  //Create media controller

  mMediaController = new MediaController(this);

  //设置MediaController

  mVideoView.setMediaController(mMediaController);

  }

  //监听MediaPlayer上报的错误信息

  @Override

  public boolean onError(MediaPlayer mp, int what, int extra) {

  // TODO Auto-generated method stub

  return false;

  }

  //Video播完的时候得到通知

  @Override

  public void onCompletion(MediaPlayer mp) {

  this.finish();

  }

  //开始

  public void onStart() {

  // Play Video

  mVideoView.setVideoURI(mUri);

  mVideoView.start();

  super.onStart();

  }

  //暂停

  public void onPause() {

  // Stop video when the activity is pause.

  mPositionWhenPaused = mVideoView.getCurrentPosition();

  mVideoView.stopPlayback();

  Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);

  Log.d(TAG, "OnStop: getDuration = " + mVideoView.getDuration());

  super.onPause();

  }

  public void onResume() {

  // Resume video player

  if(mPositionWhenPaused >= 0) {

  mVideoView.seekTo(mPositionWhenPaused);

  mPositionWhenPaused = -1;

  }

  super.onResume();

  }

  }

posted @ 2011-12-06 14:53  wenjiefeng  阅读(254)  评论(0编辑  收藏  举报