android 弹出视频

1.VideoActivity.java

package com.icc;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

/**
 * 弹出Activity
 * 调用案例
 	Intent intent = new Intent();
	intent.putExtra("video", "test.mp4");
	intent.setClass(ICCManualAppActivity.this,VideoActivity.class);
	startActivity(intent);
 * */
public class VideoActivity extends Activity implements SurfaceHolder.Callback,
OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener{
	private LinearLayout layout;
	
	private SurfaceView MySurfaceView;
	private SurfaceHolder MySurfaceHolder;
	private MediaPlayer mediaPlayer;
	private String mFileName = "";		// 视频文件全称 包含后缀 ,可以给个默认
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(R.style.Transparent);
		setContentView(R.layout.video);
		
		//获取参数
		Intent intent = getIntent();
		String myVideo = intent.getExtras().getString("video");
		if(myVideo!=null && myVideo != ""){
			mFileName = myVideo;
		}
		this.MySurfaceView = (SurfaceView)findViewById(R.id.surfaceView01);
		this.MySurfaceHolder = this.MySurfaceView.getHolder();
		this.MySurfaceHolder.addCallback(this);

		// 类型必须设置成SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS
		this.MySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		layout=(LinearLayout)findViewById(R.id.layout);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				VideoActivity.this.finish();	
			}
		});
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (this.mediaPlayer != null) {
			this.mediaPlayer.release();
			this.mediaPlayer = null;
		}
	}

	public void PlayVideo() {

		try {
			// 视频文件放在 assert中
			
			File videoFile = new File(Environment.getExternalStorageDirectory(),"Movies/"+mFileName);
			this.mediaPlayer = new MediaPlayer();
			this.mediaPlayer.setDataSource(videoFile.getAbsolutePath());

			// 视频放在SDCard中
			// File sdFile = Environment.getExternalStorageDirectory();
			// this.mediaPlayer = new MediaPlayer();
			// this.mediaPlayer.setDataSource(sdFile.getAbsoluteFile() +
			// File.separator + "welcome.3gp");

			// 视频放在res中
			// this.mediaPlayer = MediaPlayer.create(this, R.raw.welcome);

			// 网上的视频,没有测试
			// this.mediaPlayer
			// .setDataSource("http://xy2.163.com/download/down/wukong.mp3");

			this.mediaPlayer.setDisplay(this.MySurfaceHolder);
			this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			// 准备完成后才可以播放,另外如果文件特别大或者从网上获得的资源
			// 会在这里等待时间过长,造成堵塞,这样的话就得用
			// this.mediaPlayer.prepareAsync(),然后监听是否准备完毕在开始

			this.mediaPlayer.prepare();
			this.mediaPlayer.start();

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub

	}

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

	}

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

	}

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

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		finish();
		return true;
	}
}

2.video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:alpha="100"
  android:gravity="center">
    <LinearLayout
      android:gravity="center"
	  android:id="@+id/layout"
	  android:layout_width="fill_parent"
	  android:layout_height="fill_parent"
	  android:orientation="vertical">
  	  <SurfaceView 
  	  	android:gravity="center"
  		android:layout_width="fill_parent"
  		android:layout_height="fill_parent" 
  		android:id="@+id/surfaceView01"/>
	</LinearLayout>
</LinearLayout>


你可能感兴趣的:(android,exception,String,layout,null,encoding)