MediaPlayer在prepareAsync上花费很长时间,如何最小化时间

时间:2013-05-31 07:06:04

标签: android stream video-streaming media-player prepare

我的问题是应用程序在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%时,我设法节省大约1000ms。但这还不够。我不能以某种方式让它被称为更快?大约30%?

1 个答案:

答案 0 :(得分: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以及可能的早期版本兼容。额外的工作,额外的代码,毫无疑问是额外的错误......