实现简单的Android的播放视频功能
<br />http://henzil.easymorse.com/?p=165<br /> <br />实现播放视频有两种方式,一种是使用VideoView;一种是使用SurfaceView。<br /> <br />VideoView<br />在main.xml中加入:<br /><AbsoluteLayout xmlns:android="http://schemas.android.co
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;
}
}
}
更多推荐
所有评论(0)