Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fixes a leak caused by AudioManager using an Activity context, see
public class ActivityUsingVideoView extends Activity {
@Override protected void attachBaseContext(Context base) {
* Fixes a leak caused by AudioManager using an Activity context.
* Tracked at and
public class AudioServiceActivityLeak extends ContextWrapper {
AudioServiceActivityLeak(Context 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);
Copy link

albertcbraun commented Dec 22, 2015

This is truly helpful. Thank you.

Copy link

wilsonssss commented Feb 20, 2016

it works! thank you man.

Copy link

bluecoshen commented Jul 28, 2016

Great! Thank you Sir!

Copy link

yongsunCN commented Aug 5, 2016

This made my day! Awesome Work!

Copy link

vabhishek commented Sep 20, 2016

Awesome. Thanks!!

Copy link

jerrywoodz commented Nov 4, 2016

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 ?

Copy link

mrajput-systango commented Nov 28, 2019

This didn't work

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