我的问题是应用程序在 prepareAsync 中停留了大约 3-4 秒:这需要很长时间,所以我需要改变一些东西,以尽量减少它停留在那里的时间。
这是我的代码:
if (path_to_play == "") {
fragmentNavigator.setErrorMessage("notif test: Video url cannot be accessed");
}
setSurfaceHolder();
updateStatistics();
loading.setVisibility(View.VISIBLE);
break;
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(path_to_play);
if ((holder != null) && (holder.getClass() != null) && holder.getSurface().isValid()) {
mMediaPlayer.setDisplay(holder);
} else {
LogService.log(TAG, "notif test: holder: " + holder);
setSurfaceHolder();
}
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnSeekCompleteListener(this);
mMediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
LogService.log(TAG, "notif test: IllegalArgumentException called");
LogService.err(TAG, e.getMessage(), e, 1);
} catch (IllegalStateException e) {
LogService.log(TAG, "notif test: IllegalStateException called");
LogService.err(TAG, e.getMessage(), e, 1);
} catch (IOException e) {
LogService.log(TAG, "notif test: IOException called");
doCleanUp();
try {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (NullPointerException e) {
LogService.log(TAG, "notif test: NullPointerException called");
LogService.err(TAG, e.getMessage(), e, 1);
}
这就是我的 onPrepare 函数的样子:
public void onPrepared(MediaPlayer mediaplayer) {
startVideoPlayback(); //function that starts the video
}
这是 StartVideoPlayback 函数:
private void startVideoPlayback() {
LogService.log(TAG, "startVideoPlayback()");
if (holder.getSurface() == null) {
setSurfaceHolder();
}
if (record_canceled) {
wasPaused = true;
LogService.log(TAG, "waspaused intent: " + wasPaused);
}
if (/* VideoDataManager.getInstance().getVideoRecorded() || */wasPaused) {
LogService.log(TAG, "seek");
mMediaPlayer.seekTo(100);
play.setImageResource(R.drawable.play);
} else if (record_canceled) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
play.setImageResource(R.drawable.play);
} else {
LogService.log(TAG, "dont seek: " + wasPaused);
mMediaPlayer.start();
mMediaPlayer.seekTo(100);
wasPaused = false;
play.setImageResource(R.drawable.pause);
}
updateButtonStates();
updateStatistics();
loading.setVisibility(View.INVISIBLE);
if (VideoDataManager.getInstance().getVideoRecorded() && wasPaused) {
LogService.log(TAG, "------- pause the new recorded video, setVideoRecorded(false)");
// VideoDataManager.getInstance().setVideoRecorded(false);
}
if (VideoDataManager.getInstance().getVideoRecorded() && mReloadVideo) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// NetworkInfo ni = cm.getActiveNetworkInfo();
// if ((ni != null) && (ni.getState() ==
// NetworkInfo.State.CONNECTED)) {
// fragmentNavigator.uploading();
// }
mReloadVideo = false;
} else if ((VideoHolderActivity.notificationBar.getTextMessageTag(VideoHolderActivity.notificationTextMessage).equals("new") || VideoHolderActivity.notificationBar.getTextMessageTag(VideoHolderActivity.notificationTextMessage).equals("gcm")) && (all_videos.size() > 1)) {
fragmentNavigator.BackButtonForNotification();
VideoDataManager.getInstance().setVideoRecorded(false);
} else if (runOfflineReload) {
setVideoPlayback();
runOfflineReload = false;
}
}
还使用以下代码尝试了 onBufferingUpdate 侦听器,而不是 onPrepared:
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
LogService.log(TAG, "-------------------Onbuffering time: " + (System.currentTimeMillis() - startTimePrepare) + "||||percentage: " + percent);
if (isAsync) {
LogService.log(TAG, "-------------------Onbuffering time: " + (System.currentTimeMillis() - startTimePrepare));
mIsVideoReadyToBePlayed = true;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown && pm.isScreenOn()) {
startVideoPlayback();
downloadService();
mp.setOnBufferingUpdateListener(null);
isAsync = false;
}
}
}
有时,当 onBufferingUpdate 被调用两次时,一次是 100,一次是 60-80%,我设法节省了大约 1000 毫秒。但这还不够。不知何故,我不能让它更快地被调用吗?大约 30%?