Last active
August 29, 2015 14:21
-
-
Save imallan/7f3d9467bfff86935d3d to your computer and use it in GitHub Desktop.
Using Java reflection to change the Context of the AudioManager to ApplicationContext, so VideoView can be GCed properly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
//Here fix a leak when VideoView holds context | |
// because AudioManager didn't release audio focus properly. | |
if (mVideoView != null) { | |
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); | |
try { | |
Field f = am.getClass().getDeclaredField("mContext"); | |
f.setAccessible(true); | |
f.set(am, getApplicationContext()); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment