Android VideoPlayer,Android大厂技术面试题汇总

本文详细介绍了Android中VideoView组件的使用,包括如何处理视频播放、控制事件、错误监听以及音量调节。通过示例代码展示了播放、暂停、音量调整等功能的实现,并提供了完整的回调函数解析。
摘要由CSDN通过智能技术生成

// we need), so we won’t get a “surface changed” callback, so

// start the video here instead of in the callback.

if (seekWhenPrepared != 0) {

mediaPlayer.seekTo(seekWhenPrepared);

seekWhenPrepared = 0;

}

if (startWhenPrepared) {

mediaPlayer.start();

startWhenPrepared = false;

if (mediaController != null) {

mediaController.show();

}

} else if (!isPlaying() &&

(seekWhenPrepared != 0 || getCurrentPosition() > 0)) {

if (mediaController != null) {

// Show the media controls when we’re paused into a video and make 'em stick.

mediaController.show(0);

}

}

}

} else {

// We don’t know the video size yet, but should start anyway.

// The video size might be reported to us later.

if (seekWhenPrepared != 0) {

mediaPlayer.seekTo(seekWhenPrepared);

seekWhenPrepared = 0;

}

if (startWhenPrepared) {

mediaPlayer.start();

startWhenPrepared = false;

}

}

}

};

private OnCompletionListener mCompletionListener =

new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

if (mediaController != null) {

mediaController.hide();

}

if (myOnCompletionListener != null) {

myOnCompletionListener.onCompletion(mediaPlayer);

}

}

};

private OnErrorListener mErrorListener =

new OnErrorListener() {

public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {

Log.d(TAG, "Error: " + framework_err + “,” + impl_err);

if (mediaController != null) {

mediaController.hide();

}

/* If an error handler has been supplied, use it and finish. */

if (myOnErrorListener != null) {

if (myOnErrorListener.onError(mediaPlayer, framework_err, impl_err)) {

return true;

}

}

/* Otherwise, pop up an error dialog so the user knows that

  • something bad has happened. Only try and pop up the dialog

  • if we’re attached to a window. When we’re going away and no

  • longer have a window, don’t bother showing the user an error.

*/

if (getWindowToken() != null) {

Resources r = mContext.getResources();

int messageId;

/* if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {

messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback;

} else {

messageId = com.android.internal.R.string.VideoView_error_text_unknown;

}

new AlertDialog.Builder(mContext)

.setTitle(com.android.internal.R.string.VideoView_error_title)

.setMessage(messageId)

.setPositiveButton(com.android.internal.R.string.VideoView_error_button,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

If we get here, there is no onError listener, so

  • at least inform them that the video is over.

if (mOnCompletionListener != null) {

mOnCompletionListener.onCompletion(mMediaPlayer);

}

}

})

.setCancelable(false)

.show();*/

}

return true;

}

};

private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener =

new MediaPlayer.OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {

currentBufferPercentage = percent;

}

};

/**

  • Register a callback to be invoked when the media file

  • is loaded and ready to go.

  • @param l The callback that will be run

*/

public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {

myOnPreparedListener = l;

}

/**

  • Register a callback to be invoked when the end of a media file

  • has been reached during playback.

  • @param l The callback that will be run

*/

public void setOnCompletionListener(OnCompletionListener l) {

myOnCompletionListener = 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.

  • @param l The callback that will be run

*/

public void setOnErrorListener(OnErrorListener l) {

myOnErrorListener = l;

}

SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() {

public void surfaceChanged(SurfaceHolder holder, int format,

int w, int h) {

surfaceWidth = w;

surfaceHeight = h;

if (mediaPlayer != null && isPrepared && videoWidth == w && videoHeight == h) {

if (seekWhenPrepared != 0) {

mediaPlayer.seekTo(seekWhenPrepared);

seekWhenPrepared = 0;

}

mediaPlayer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值