android mediaplay videoview mediacontrol 網路視訊

心鑫發表於2013-11-16
package com.example.media;

import java.io.IOException;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
import android.widget.MediaController.MediaPlayerControl;

public class MyMediaPlayer implements OnCompletionListener, OnErrorListener,
		OnInfoListener, OnPreparedListener, OnSeekCompleteListener,
		OnVideoSizeChangedListener, OnBufferingUpdateListener,
		SurfaceHolder.Callback, MediaPlayerControl {
	MediaPlayer mp;
	Context context;
	VideoView videoView;
	MediaController mc;

	public MyMediaPlayer(Context context) {
		this(context, null);
	}

	public MyMediaPlayer(Context context, VideoView sfView) {
		this.context = context;
		this.videoView = sfView;
		// init2(context);
		// init1();
		init3();
		mp.setOnCompletionListener(this);
		mp.setOnErrorListener(this);
		mp.setOnInfoListener(this);
		mp.setOnPreparedListener(this);
		mp.setOnSeekCompleteListener(this);
		// mp.setOnTimedTextListener(this);
		mp.setOnVideoSizeChangedListener(this);
		mp.setOnBufferingUpdateListener(this);
		mp.setScreenOnWhilePlaying(true);
		mc = new MediaController(context);
		mc.setMediaPlayer(this);

	}

	public MyMediaPlayer(VideoView sf) {
		this.videoView = sf;
	}

	private void init3() {
		mp = new MediaPlayer();
		try {
			// 從原始碼可看出返回的值是空
			// SurfaceHolder holder = videoView.getHolder();
			// http://daily3gp.com/vids/family_guy_penis_car.3gp
			mp.setDataSource(Constants.gpurl);
			mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mp.prepareAsync();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void init2(Context context) {
		// mp = MediaPlayer.create(context, R.raw.p);
	}

	private void init1() {
		mp = new MediaPlayer();
		try {
			mp.setDataSource(Constants.mp3Url);
			mp.prepareAsync();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		printLog("onVideoSizeChanged");

	}

	@Override
	public void onSeekComplete(MediaPlayer mp) {
		printLog("onSeekComplete");

	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		printLog("onPrepared");

		autoStart();

	}

	private void autoStart() {
		mc.setAnchorView(videoView.getRootView());
		mc.setEnabled(true);
		mc.show(10 * 60 * 1000);
		if (mp != null) {
			mp.start();
			return;
		}
		if (videoView != null) {
			printLog("autoStart" + videoView.getHeight() + "-"
					+ videoView.getWidth());
			videoView.start();
		}

	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		printLog("onBufferingUpdate-->" + what + "--" + extra);
		return false;
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		printLog("onBufferingUpdate-->" + what + "--" + extra);
		return false;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		printLog("onBufferingUpdate");

	}

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		printLog("onBufferingUpdate-->" + percent + "--width:height-->"
				+ mp.getVideoWidth() + ":" + mp.getVideoHeight());

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		printLog("surfaceCreated");
		// autoStart();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		printLog("surfaceChanged");
		mp.setDisplay(holder);

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		printLog("surfaceDestroyed");

	}

	private void printLog(String s) {
		Log.d("PlayerListener", s);
	}

	@Override
	public void start() {
		mp.start();

	}

	@Override
	public void pause() {
		mp.pause();

	}

	@Override
	public int getDuration() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCurrentPosition() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void seekTo(int pos) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isPlaying() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getBufferPercentage() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean canPause() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean canSeekBackward() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean canSeekForward() {
		// TODO Auto-generated method stub
		return false;
	}
}

相關文章