Skip to content

Instantly share code, notes, and snippets.

@jankovd
Last active September 22, 2020 09:41
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jankovd/891d96f476f7a9ce24e2 to your computer and use it in GitHub Desktop.
Save jankovd/891d96f476f7a9ce24e2 to your computer and use it in GitHub Desktop.
Fixes a leak caused by AudioManager using an Activity context, see https://github.com/square/leakcanary/issues/205
public class ActivityUsingVideoView extends Activity {
@Override protected void attachBaseContext(Context base) {
super.attachBaseContext(AudioServiceActivityLeak.preventLeakOf(base));
}
}
/**
* Fixes a leak caused by AudioManager using an Activity context.
* Tracked at https://android-review.googlesource.com/#/c/140481/1 and
* https://github.com/square/leakcanary/issues/205
*/
public class AudioServiceActivityLeak extends ContextWrapper {
AudioServiceActivityLeak(Context base) {
super(base);
}
public static ContextWrapper preventLeakOf(Context base) {
return new AudioServiceActivityLeak(base);
}
@Override public Object getSystemService(String name) {
if (Context.AUDIO_SERVICE.equals(name)) {
return getApplicationContext().getSystemService(name);
}
return super.getSystemService(name);
}
}
@jerrywoodz
Copy link

I use above attachBaseContext & AudioServiceActivityLeak for my video player (extend VideoView) plays 25 of small video files with duration between 15 seconds and 32 seconds in a media box with Android 4.4.2 forever, the MediaPlayer of Android 4.4.2 crashes after 14 hours, I did two times for this testing, I got same result.
If I do not use attachBaseContext & AudioServiceActivityLeak for my video player, then my video player stops to play these 25 of video files after 27 hours (not crash)
can anyone help me explain why ?

@mrajput-systango
Copy link

This didn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment