gpt4 book ai didi

android - MediaPlayer 在 prepareAsync 上需要很长时间,如何最大限度地减少时间

转载 作者:太空狗 更新时间:2023-10-29 14:21:02 27 4
gpt4 key购买 nike

我的问题是应用程序在 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 Listener,而不是 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%?

最佳答案

if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(....);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
playButton.setClickable(true);
mp.start();
}
});
}

编辑

我认为服务器端存在一些兼容性问题。这很奇怪,因为模拟器在我的情况下可以正常处理 - 只是不在我的 Froyo Galaxy S 上,即使它是相同的 API 版本。

这可能是编解码器问题,http 流问题,我不知道。但是所有出现故障的服务器往往都是旧服务器,底部是“Copyright 1998 - 2004”......您可能认为这不是最近或最新的。

一种潜在的解决方法(我还没有尝试过)是使用 StreamProxy,这也将使您的代码与 2.1 和可能的早期版本兼容。以额外的工作、额外的代码和毫无疑问的额外错误为代价......

关于android - MediaPlayer 在 prepareAsync 上需要很长时间,如何最大限度地减少时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16851621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com