棉花糖中的Exoplayer问题

时间:2018-08-16 19:56:40

标签: android-6.0-marshmallow exoplayer exoplayer2.x

这是我的Exoplayer代码。ExoplayerVideoHandler是Singleton类。在Nogut或更高版本的设备中,当我从一个片段转到另一个活动时,exoplayer OnBackGround()和OnForground()可以正常工作。但是在棉花糖或以下设备中,此代码无法正常工作。在棉花糖中,将调用finalize(),并且播放器将为null。有人可以帮我吗?

public static ExoPlayerVideoHandler instance;
public static SimpleExoPlayer player;
private Uri playerUri;
private boolean isPlayerPlaying;
private static ComponentListener componentListener;
private static Handler mHandler = new Handler(Looper.getMainLooper());
private static CustomPlayerView playerView;
private static ProgressBar progressBar;
private static long pos;
private int playbackPosition = 0;
private int currentWindow = 0;
private boolean playWhenReady = true;
private static final String TAG = ExoPlayerVideoHandler.class.getName();

public static ExoPlayerVideoHandler getInstance(CustomPlayerView exoPlayerView, ProgressBar mVideoProgressBar) {
    if (instance == null) {
        instance = new ExoPlayerVideoHandler();
        componentListener = new ComponentListener();
        playerView = exoPlayerView;
        progressBar = mVideoProgressBar;
    }
    return instance;
}

public void releaseVideoPlayer() {
    if (player != null) {
        player.release();
    }
    player = null;
    instance = null;
}

public void releaseVideoPlayer(Activity activity, FrameLayout mVideoFloatContainer, View mCurrentPlayArea) {
    if (player != null) {
        player.release();
    }
    mVideoFloatContainer.setVisibility(View.INVISIBLE);
    if (activity.getWindow() != null)
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (mCurrentPlayArea != null) {
        mCurrentPlayArea.setClickable(true);
    }
    player = null;
    instance = null;
}

/**
 * Runnable for update current video progress
 * 1.start this runnable in {@link @MediaPlayerWrapper.MainThreadMediaPlayerListener#onInfoMainThread(int)}
 * 2.stop(remove) this runnable in {@link @MediaPlayerWrapper.MainThreadMediaPlayerListener#onVideoStoppedMainThread()}
 * {@link @MediaPlayerWrapper.MainThreadMediaPlayerListener#onVideoCompletionMainThread()}
 * {@link @MediaPlayerWrapper.MainThreadMediaPlayerListener#onErrorMainThread(int, int)} ()}
 */
private static Runnable mProgressRunnable = new Runnable() {
    @Override
    public void run() {
        if (player != null) {
            if (playerView.controllerVisible) {
                progressBar.setVisibility(View.GONE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
            }
            long position = player.getCurrentPosition();
            long duration = player.getDuration();
            if (duration != 0) {
                pos = 1000L * position / duration;
                int percent = player.getBufferedPercentage() * 10;
                progressBar.setProgress((int) pos);
                progressBar.setSecondaryProgress(percent);
                mHandler.postDelayed(this, 1000);
            }
        }
    }
};


@Override
protected void finalize() throws Throwable {

    LogUtils.logMessageError(TAG,"FINALIZE CALLED"+player);
}

public void prepareExoPlayerForUri(Context activity, Uri uri) {
    LogUtils.logMessageError(TAG, "prepareExoPlayerForUri==" + uri);
    if (activity != null && uri != null && playerView != null) {
        if (!uri.equals(playerUri) || player == null) {
            player = ExoPlayerFactory.newSimpleInstance(
                    new DefaultRenderersFactory(activity),
                    new DefaultTrackSelector(), new DefaultLoadControl());
            playerView.setPlayer(player);
            player.addListener(componentListener);
            player.setPlayWhenReady(playWhenReady);
            player.seekTo(currentWindow, playbackPosition);
            playerUri = uri;
            MediaSource mediaSource = buildMediaSource(uri);
            player.prepare(mediaSource, true, false);
            playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
            player.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        }
        player.clearVideoSurface();
        player.setVideoSurfaceView(
                (SurfaceView) playerView.getVideoSurfaceView());
        player.seekTo(player.getCurrentPosition() + 1);
        playerView.setPlayer(player);
    }
}

private MediaSource buildMediaSource(Uri uri) {
    LogUtils.logMessageError(TAG, "buildMediaSourceUri==" + uri);
    return new ExtractorMediaSource.Factory(
            new DefaultHttpDataSourceFactory("exoplayer")).
            createMediaSource(uri);
}

public void goToBackground() {
    if (player != null) {
        isPlayerPlaying = player.getPlayWhenReady();
        player.setPlayWhenReady(false);
    }
}

public void goToForeground(CustomPlayerView exoPlayerView) {
    if (player != null) {

        playerView = exoPlayerView;
        player.setPlayWhenReady(isPlayerPlaying);
        player.setPlayWhenReady(playWhenReady);
        exoPlayerView.setPlayer(player);
    }
}

public void pausePlayer() {
    if (player != null) {
        player.setPlayWhenReady(false);
        player.getPlaybackState();
    }
}

public void startPlayer() {
    if (player != null) {
        player.setPlayWhenReady(true);
        player.getPlaybackState();
    }
}

private static class ComponentListener extends Player.DefaultEventListener {
    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        super.onPlayerStateChanged(playWhenReady, playbackState);
        String stateString;
        switch (playbackState) {
            case Player.STATE_IDLE:
                stateString = "ExoPlayer.STATE_IDLE      -";
                mHandler.post(mProgressRunnable);
                break;
            case Player.STATE_BUFFERING:
                stateString = "ExoPlayer.STATE_BUFFERING -";
                break;
            case Player.STATE_READY:
                stateString = "ExoPlayer.STATE_READY     -";
                mHandler.post(mProgressRunnable);
                break;
            case Player.STATE_ENDED:
                stateString = "ExoPlayer.STATE_ENDED     -";
                mHandler.removeCallbacks(mProgressRunnable);
                progressBar.setVisibility(View.GONE);
                EventBus.getDefault().postSticky(new VideoShareEvent(true));
                EventBus.getDefault().postSticky(new VideoShareForDetailsEvent(true));
                break;
            default:
                stateString = "UNKNOWN_STATE             -";
                mHandler.removeCallbacks(mProgressRunnable);

                break;
        }
        Log.e("manu", "changed state to " + stateString
                + " playWhenReady: " + playWhenReady);
    }
}

}

0 个答案:

没有答案