Fields Summary |
---|
private String | TAG |
private android.net.Uri | mUri |
private int | mDuration |
private android.view.SurfaceHolder | mSurfaceHolder |
private android.media.MediaPlayer | mMediaPlayer |
private boolean | mIsPrepared |
private int | mVideoWidth |
private int | mVideoHeight |
private int | mSurfaceWidth |
private int | mSurfaceHeight |
private MediaController | mMediaController |
private android.media.MediaPlayer.OnCompletionListener | mOnCompletionListener |
private MediaPlayer.OnPreparedListener | mOnPreparedListener |
private int | mCurrentBufferPercentage |
private android.media.MediaPlayer.OnErrorListener | mOnErrorListener |
private boolean | mStartWhenPrepared |
private int | mSeekWhenPrepared |
MediaPlayer.OnVideoSizeChangedListener | mSizeChangedListener |
MediaPlayer.OnPreparedListener | mPreparedListener |
private android.media.MediaPlayer.OnCompletionListener | mCompletionListener |
private android.media.MediaPlayer.OnErrorListener | mErrorListener |
private MediaPlayer.OnBufferingUpdateListener | mBufferingUpdateListener |
SurfaceHolder.Callback | mSHCallback |
Methods Summary |
---|
private void | attachMediaController()
if (mMediaPlayer != null && mMediaController != null) {
mMediaController.setMediaPlayer(this);
View anchorView = this.getParent() instanceof View ?
(View)this.getParent() : this;
mMediaController.setAnchorView(anchorView);
mMediaController.setEnabled(mIsPrepared);
}
|
public int | getBufferPercentage()
if (mMediaPlayer != null) {
return mCurrentBufferPercentage;
}
return 0;
|
public int | getCurrentPosition()
if (mMediaPlayer != null && mIsPrepared) {
return mMediaPlayer.getCurrentPosition();
}
return 0;
|
public int | getDuration()
if (mMediaPlayer != null && mIsPrepared) {
if (mDuration > 0) {
return mDuration;
}
mDuration = mMediaPlayer.getDuration();
return mDuration;
}
mDuration = -1;
return mDuration;
|
private void | initVideoView()
mVideoWidth = 0;
mVideoHeight = 0;
getHolder().addCallback(mSHCallback);
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
|
public boolean | isPlaying()
if (mMediaPlayer != null && mIsPrepared) {
return mMediaPlayer.isPlaying();
}
return false;
|
public boolean | onKeyDown(int keyCode, android.view.KeyEvent event)
if (mIsPrepared &&
keyCode != KeyEvent.KEYCODE_BACK &&
keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
keyCode != KeyEvent.KEYCODE_MENU &&
keyCode != KeyEvent.KEYCODE_CALL &&
keyCode != KeyEvent.KEYCODE_ENDCALL &&
mMediaPlayer != null &&
mMediaController != null) {
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
if (mMediaPlayer.isPlaying()) {
pause();
mMediaController.show();
} else {
start();
mMediaController.hide();
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
&& mMediaPlayer.isPlaying()) {
pause();
mMediaController.show();
} else {
toggleMediaControlsVisiblity();
}
}
return super.onKeyDown(keyCode, event);
|
protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec)
//Log.i("@@@@", "onMeasure");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if ( mVideoWidth * height > width * mVideoHeight ) {
//Log.i("@@@", "image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
} else if ( mVideoWidth * height < width * mVideoHeight ) {
//Log.i("@@@", "image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
} else {
//Log.i("@@@", "aspect ratio is correct: " +
//width+"/"+height+"="+
//mVideoWidth+"/"+mVideoHeight);
}
}
//Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
|
public boolean | onTouchEvent(android.view.MotionEvent ev)
if (mIsPrepared && mMediaPlayer != null && mMediaController != null) {
toggleMediaControlsVisiblity();
}
return false;
|
public boolean | onTrackballEvent(android.view.MotionEvent ev)
if (mIsPrepared && mMediaPlayer != null && mMediaController != null) {
toggleMediaControlsVisiblity();
}
return false;
|
private void | openVideo()
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// Tell the music playback service to pause
// TODO: these constants need to be published somewhere in the framework.
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
mContext.sendBroadcast(i);
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(mPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
mIsPrepared = false;
Log.v(TAG, "reset duration to -1 in openVideo");
mDuration = -1;
mMediaPlayer.setOnCompletionListener(mCompletionListener);
mMediaPlayer.setOnErrorListener(mErrorListener);
mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
mCurrentBufferPercentage = 0;
mMediaPlayer.setDataSource(mContext, mUri);
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.prepareAsync();
attachMediaController();
} catch (IOException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
return;
} catch (IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
return;
}
|
public void | pause()
if (mMediaPlayer != null && mIsPrepared) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
}
mStartWhenPrepared = false;
|
public int | resolveAdjustedSize(int desiredSize, int measureSpec)
int result = desiredSize;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
/* Parent says we can be as big as we want. Just don't be larger
* than max size imposed on ourselves.
*/
result = desiredSize;
break;
case MeasureSpec.AT_MOST:
/* Parent says we can be as big as we want, up to specSize.
* Don't be larger than specSize, and don't be larger than
* the max size imposed on ourselves.
*/
result = Math.min(desiredSize, specSize);
break;
case MeasureSpec.EXACTLY:
// No choice. Do what we are told.
result = specSize;
break;
}
return result;
|
public void | seekTo(int msec)
if (mMediaPlayer != null && mIsPrepared) {
mMediaPlayer.seekTo(msec);
} else {
mSeekWhenPrepared = msec;
}
|
public void | setMediaController(MediaController controller)
if (mMediaController != null) {
mMediaController.hide();
}
mMediaController = controller;
attachMediaController();
|
public void | setOnCompletionListener(android.media.MediaPlayer.OnCompletionListener l)Register a callback to be invoked when the end of a media file
has been reached during playback.
mOnCompletionListener = l;
|
public void | setOnErrorListener(android.media.MediaPlayer.OnErrorListener l)Register a callback to be invoked when an error occurs
during playback or setup. If no listener is specified,
or if the listener returned false, VideoView will inform
the user of any errors.
mOnErrorListener = l;
|
public void | setOnPreparedListener(MediaPlayer.OnPreparedListener l)Register a callback to be invoked when the media file
is loaded and ready to go.
mOnPreparedListener = l;
|
public void | setVideoPath(java.lang.String path)
setVideoURI(Uri.parse(path));
|
public void | setVideoURI(android.net.Uri uri)
mUri = uri;
mStartWhenPrepared = false;
mSeekWhenPrepared = 0;
openVideo();
requestLayout();
invalidate();
|
public void | start()
if (mMediaPlayer != null && mIsPrepared) {
mMediaPlayer.start();
mStartWhenPrepared = false;
} else {
mStartWhenPrepared = true;
}
|
public void | stopPlayback()
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
|
private void | toggleMediaControlsVisiblity()
if (mMediaController.isShowing()) {
mMediaController.hide();
} else {
mMediaController.show();
}
|