My issue is that the app stays about 3-4 seconds in the prepareAsync: This takes to long, so i need to change something, to minimize the time it stays there.
This is my code:
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);
}
This is what my onPrepare function looks like:
public void onPrepared(MediaPlayer mediaplayer) {
startVideoPlayback(); //function that starts the video
}
This is the StartVideoPlayback function:
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;
}
}
Also tried the onBufferingUpdate Listener, instead of the onPrepared, using this code:
@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;
}
}
}
Sometimes, when the onBufferingUpdate is called twice, once at 100 and once about at 60-80%, i manage to save about 1000ms. But this is not enough. Can't i make it be called faster, somehow? at about 30%?