Android 视频视图内存泄漏
你们有没有遇到过类似的内存泄漏? 这就是我目前处理视频视图的方式Android 视频视图内存泄漏,android,memory-leaks,android-videoview,Android,Memory Leaks,Android Videoview,你们有没有遇到过类似的内存泄漏? 这就是我目前处理视频视图的方式 @Override protected void onFinishInflate() { super.onFinishInflate(); ButterKnife.bind(this); Uri videoUri = Uri.parse(String.format("android.resource://%s/%s", getContext().getPackageName(), videoRes));
@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
Uri videoUri = Uri.parse(String.format("android.resource://%s/%s", getContext().getPackageName(), videoRes));
videoView.setVideoURI(videoUri);
videoView.setOnPreparedListener(mp -> {
mp.setLooping(true);
videoView.start();
});
}
这就是我从金丝雀身上得到的
感谢您的帮助 对片段使用ButterKnife时,需要使用
onDestroyView()
中的Unbinder
来正确地取消引用片段的视图,因为片段与活动具有不同的生命周期
这是一个相关的问题。如果您正在使用Butterknife,请确保解除绑定,如果您不确保在OnDestry中调用videoView.stopPlayback()这为我在一个不使用Butterknife的
AppCompative
中修复了它
步骤1:创建实用程序类
public class AudioServiceContext extends ContextWrapper {
public AudioServiceContext(Context base) {
super(base);
}
public static ContextWrapper getContext(Context base) {
return new AudioServiceContext(base);
}
@Override
public Object getSystemService(String name) {
if (Context.AUDIO_SERVICE.equals(name)) {
return getApplicationContext().getSystemService(name);
}
return super.getSystemService(name);
}
}
public class YourVideoActivity extends AppCompatActivity {
private VideoView videoView;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(AudioServiceContext.getContext(newBase));
}
//etc...
}
步骤2:在活动中使用此实用程序类
public class AudioServiceContext extends ContextWrapper {
public AudioServiceContext(Context base) {
super(base);
}
public static ContextWrapper getContext(Context base) {
return new AudioServiceContext(base);
}
@Override
public Object getSystemService(String name) {
if (Context.AUDIO_SERVICE.equals(name)) {
return getApplicationContext().getSystemService(name);
}
return super.getSystemService(name);
}
}
public class YourVideoActivity extends AppCompatActivity {
private VideoView videoView;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(AudioServiceContext.getContext(newBase));
}
//etc...
}
信用证:调用
videoView.suspend()
inonPause()
或onDestory()
ButterKnife.bind
返回Unbinder
。将其保留为成员并可以解除绑定。解除绑定onDetacheFromWindow
并检查是否存在相同的泄漏我既没有使用Butterknife也没有使用片段-此外,在onCompletion(MediaPlayer mp)
回调中,有一个MVideView.stopLayback()
调用stopLayback()
似乎还不够好。我得到同样的内存泄漏,即使这样。欢迎使用堆栈溢出!感谢您提供这段代码片段,它可能会提供一些有限的、即时的帮助。A通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有其他类似问题的读者更有用。请在你的答案中添加一些解释,包括你所做的假设。这是一个正确的答案。这里写的解释