`
弄月吟风
  • 浏览: 199193 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Android视频播放

阅读更多

视频播放优好多种,一种是播放系统可以播放的视频文件,一种是播放动态码流,前一种比较简单,后一种需要对视频进行解码,然后才能播放,今天就先写上前一种,后一种需要Jni进行AndroidNDK编程

android有两个组建可以用来观看视频,一个是VideoView一个是SurfaceView;

用VideoView观看视频:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <VideoView
        android:id="@+id/video"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

 源代码:

public class VideoTestActivity extends Activity {
	/** Called when the activity is first created. */
	private VideoView videoView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findID();
	}

	private void findID() {
		videoView = (VideoView) findViewById(R.id.video);
		// videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/moto_0012.3gp");//本地资源
		videoView.setVideoURI(Uri
				.parse("http://172.16.108.151/video/test1.mp4"));// 网络资源
		videoView
				.setMediaController(new MediaController(VideoTestActivity.this));// 设置模式,播放进度条
		videoView.requestFocus();
		videoView.start();
	}
}

 用SurfaceView播放

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<SurfaceView android:id="@+id/surfaceView"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center">
	</SurfaceView>
</LinearLayout>

 Java代码

package com.Aina.Android;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Test extends Activity implements OnBufferingUpdateListener,
		OnCompletionListener, MediaPlayer.OnPreparedListener,
		SurfaceHolder.Callback {
	private int width = 0;
	private int height = 0;
	private MediaPlayer mMediaPlayer = null;
	private SurfaceView mSurfaceView = null;
	private SurfaceHolder holder = null;
	private String path = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mSurfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
		holder = mSurfaceView.getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置风格
	}

	public void playVedio() {
		try {
			path = android.os.Environment.getExternalStorageDirectory()
					+ "/moto_0012.3gp";
			mMediaPlayer = new MediaPlayer();
//			mMediaPlayer.setDataSource(path);
			mMediaPlayer.setDataSource(this, Uri.parse("http://192.168.0.132:8080/mp.mp4"));
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mMediaPlayer.setDisplay(holder);
			mMediaPlayer.prepare();// 准备
			Log.i("TAG-Duration", mMediaPlayer.getDuration()+"");
			mMediaPlayer.setOnBufferingUpdateListener(this);
			mMediaPlayer.setOnCompletionListener(this);
			mMediaPlayer.setOnPreparedListener(this);
		} catch (Exception ex) {

		}
	}

	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		// TODO Auto-generated method stub
		Log.i("TAG-onBufferingUpdate", percent+"|"+mMediaPlayer.getCurrentPosition());
	}

	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub
		Log.i("TAG-onCompletion", "Completion");
	}

	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub
		width = mMediaPlayer.getVideoWidth();
		height = mMediaPlayer.getVideoHeight();
		if(width !=0 && height !=0){
			holder.setFixedSize(width, height);//设置视频高宽
			mMediaPlayer.start();
			Log.i("TAG-Duration2", mMediaPlayer.getDuration()+"");
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		playVedio();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.i("TAG-surfaceDestroyed", "surfaceDestroyed");
	}

	@Override
	protected void onPause() {
		super.onPause();
		if(mMediaPlayer != null){
			if(mMediaPlayer.isPlaying()){
				mMediaPlayer.stop();
			}
			mMediaPlayer.reset();
			mMediaPlayer.release();
			mMediaPlayer = null;
		}
	}
	
}
 
分享到:
评论
2 楼 f839903061 2012-08-01  
playVedio()方法在哪里调用了?我在create()方法中调用依然没显示画面!
1 楼 f839903061 2012-08-01  
我用surfaceview播放动画没有显示出画面!

相关推荐

Global site tag (gtag.js) - Google Analytics