http://henzil.easymorse.com/?p=165

实现播放视频有两种方式,一种是使用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文件中加入:

<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;
}
}
}

更多相关文章

  1. Android UI系列:关于按钮点击事件
  2. android 使用gdb调试的方式
  3. android 播放视频
  4. Android 中带有进度条效果的按钮(Button)
  5. android中设置activity的出现方式
  6. android 超简单的拖动按钮 悬浮按钮 吸附按钮
  7. Android WebView加载H5音视频自动播放、关闭Activity停止播放
  8. Android五种存储方式
  9. Android Studio 新建编辑条 点击按钮显示控件中的内容

随机推荐

  1. 浅谈 Android(安卓)开发文化
  2. Android(安卓)玩转PathMeasure之自定义支
  3. 【原】[webkit移动开发笔记]之兼容iPhone
  4. Android--设置软键盘的显示和隐藏
  5. Android中截图(surfaceView)
  6. Android系统篇之----Android中的run-as命
  7. 【转发】Android(安卓)Metro风格的Launch
  8. Android(安卓)Studio 模板用法与自定义模
  9. android构建自定义的视图组件onMeasure
  10. Android中资源文件(非代码部分)的使用概