Skip to content

Instantly share code, notes, and snippets.

@imallan
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imallan/7f3d9467bfff86935d3d to your computer and use it in GitHub Desktop.
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.
@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