3

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

4

1 回答 1

1
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 以及可能的更早版本兼容。以额外的工作、额外的代码和毫无疑问的额外错误为代价......

于 2013-05-31T11:50:30.043 回答