gpt4 book ai didi

android - VideoView 泄漏 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:35 24 4
gpt4 key购买 nike

我在 VideoActivity 中使用以下代码在应用中显示视频:

binding.videoView.setVideoURI(videoUri);
binding.videoView.requestFocus();
binding.videoView.start();

binding.videoView.setOnPreparedListener(new
MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
binding.progressBar.progressOverlay.setVisibility(View.GONE);
}
});

binding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
binding.videoView.suspend();
binding.videoView.stopPlayback();
setResult(RESULT_OK);
finish();
}
});

binding.videoSkipButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.videoView.suspend();
binding.videoView.stopPlayback();
setResult(RESULT_OK);
finish();
}
});

在跳过/结束后,我从 leak canary 收到一份报告,说我的 Activity : enter image description here

如何消除这种泄漏?

我也看到过类似 leakcanary trace 的这个问题:VideoView memory leak

但问题在于 ButterKnife 解除绑定(bind)。我是否需要对 DataBinding 做类似的事情?

最佳答案

在您的 Activity 的 onPause()、onResume() 和 onDestroy() 方法中添加 VideoView 的 pause()、resume() 和 stopPlayback():

@Override
protected void onResume() {
binding.videoView.resume();
super.onResume();
}

@Override
protected void onPause() {
binding.videoView.pause();
super.onPause();
}

@Override
protected void onDestroy() {
binding.videoView.stopPlayback();
super.onDestroy();
}

希望对您有所帮助。谢谢:)

关于android - VideoView 泄漏 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44157619/

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