首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动平台 >

实现简单的Android的播放视频功能,该怎么解决

2012-05-22 
实现简单的Android的播放视频功能转载自:http://dev.10086.cn/cmdn/bbs/thread-70910-1-4.html实现播放视

实现简单的Android的播放视频功能
转载自:http://dev.10086.cn/cmdn/bbs/thread-70910-1-4.html

实现播放视频有两种方式,一种是使用VideoView;一种是使用SurfaceView。
VideoView

在main.xml中加入:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="fill_parent">
  <VideoView android:id="@+id/videoView" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:layout_x="10px"
  android:layout_y="10px" />
</AbsoluteLayout>
在类中,加入以下代码
super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
  setContentView(R.layout.main);
  videoView = (VideoView) findViewById(R.id.videoView);
// videoView.setVideoPath("/sdcard/xyx.3gp");
  videoView.setVideoURI(Uri.parse("/sdcard/beijinghuanyingni.mp4"));
  MediaController mediaController = new MediaController(this);
  videoView.setMediaController(mediaController);
  videoView.start();
  //videoView.requestFocus();
首先我使用的是全屏,强制横屏播放视频。.setVideoURI是设置视频的路径,这里我用的是sdcard上的视频,如果用http的,则需写完整路径。最重要的一点,我看网上有用videoView.requestFocus();来加载视屏播放,这里我没有能播放成功,改用了 videoView.start();来播放。
这种方式是使用Android自带的按钮,无需自定义暂停、播放等按钮控件。

源码见:http://henzil.googlecode.com/svn/trunk/play/SurfaceView

这种方式是使用自定义的暂停、播放等按钮控件。

在main.xml文件中加入:
在Java类中,实现OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener,SurfaceHolder.Callback接口

<SurfaceView android:id="@+id/surface"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center">
</SurfaceView>


在Java类中,实现OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener,SurfaceHolder.Callback接口
  package com.play2;

  import java.io.IOException;

  import android.app.Activity;
  import android.content.pm.ActivityInfo;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.media.MediaPlayer.OnBufferingUpdateListener;
  import android.media.MediaPlayer.OnCompletionListener;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.Window;

  public class PlayDemo2 extends Activity implements OnBufferingUpdateListener,
  OnCompletionListener, MediaPlayer.OnPreparedListener,
  SurfaceHolder.Callback {
  private MediaPlayer mediaPlayer;
  private SurfaceView surfaceView;
  private SurfaceHolder surfaceHolder;
  private int videoWidth;
  private int videoHeight;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
  this.surfaceView = (SurfaceView) this.findViewById(R.id.surface);
  this.surfaceHolder = this.surfaceView.getHolder();
  this.surfaceHolder.addCallback(this);


  this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  Log.v("cat", ">>>create ok.");
  }

  private void playVideo() throws IllegalArgumentException,
  IllegalStateException, IOException {
  this.mediaPlayer = new MediaPlayer();
  this.mediaPlayer
  .setDataSource("/sdcard/daoxiang.3gp");
  this.mediaPlayer.setDisplay(this.surfaceHolder);
  this.mediaPlayer.prepare();
  this.mediaPlayer.setOnBufferingUpdateListener(this);
  this.mediaPlayer.setOnPreparedListener(this);
  this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  Log.i("mplayer", ">>>play video");
  }

  @Override
  public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  Log.i("cat", ">>>surface changed");

  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
  try {
  this.playVideo();
  } catch (Exception e) {
  Log.i("cat", ">>>error", e);
  }
  Log.i("cat", ">>>surface created");

  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
  Log.v("mplayer", ">>>surface destroyed");

  }

  @Override
  public void onCompletion(MediaPlayer arg0) {
  // TODO Auto-generated method stub

  }

  @Override
  public void onBufferingUpdate(MediaPlayer mp, int percent) {
  // TODO Auto-generated method stub

  }

  @Override
  public void onPrepared(MediaPlayer arg0) {
  this.videoWidth = this.mediaPlayer.getVideoWidth();
  this.videoHeight = this.mediaPlayer.getVideoHeight();

  if (this.videoHeight != 0 && this.videoWidth != 0) {
  this.surfaceHolder.setFixedSize(this.videoWidth, this.videoHeight);
  this.mediaPlayer.start();
  }

  }
  @Override
  protected void onDestroy() {
  super.onDestroy();
  if (this.mediaPlayer != null) {
  this.mediaPlayer.release();
  this.mediaPlayer = null;
  }
  }
  }
源码见:http://henzil.googlecode.com/svn/trunk/play2/

[解决办法]

热点排行