Merge pull request #1328 from linguokun1/master

solve the memory leak on Android and avoid the crash on kitkat
This commit is contained in:
Hampton Maxwell 2018-12-13 12:50:25 -08:00 committed by GitHub
commit 52334f031a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -235,12 +235,19 @@ public class ReactVideoView extends ScalableVideoView implements
mediaController.hide(); mediaController.hide();
} }
if ( mMediaPlayer != null ) { if ( mMediaPlayer != null ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mMediaPlayer.setOnTimedMetaDataAvailableListener(null);
}
mMediaPlayerValid = false; mMediaPlayerValid = false;
release(); release();
} }
if (mIsFullscreen) { if (mIsFullscreen) {
setFullscreen(false); setFullscreen(false);
} }
if (mThemedReactContext != null) {
mThemedReactContext.removeLifecycleEventListener(this);
mThemedReactContext = null;
}
} }
public void setSrc(final String uriString, final String type, final boolean isNetwork, final boolean isAsset, final ReadableMap requestHeaders) { public void setSrc(final String uriString, final String type, final boolean isNetwork, final boolean isAsset, final ReadableMap requestHeaders) {
@ -567,8 +574,7 @@ public class ReactVideoView extends ScalableVideoView implements
}); });
} }
// Select track (so we can use it to listen to timed meta data updates) selectTimedMetadataTrack(mp);
mp.selectTrack(0);
} }
@Override @Override
@ -603,9 +609,7 @@ public class ReactVideoView extends ScalableVideoView implements
@Override @Override
public void onBufferingUpdate(MediaPlayer mp, int percent) { public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Select track (so we can use it to listen to timed meta data updates) selectTimedMetadataTrack(mp);
mp.selectTrack(0);
mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0); mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0);
} }
@ -761,4 +765,20 @@ public class ReactVideoView extends ScalableVideoView implements
return result; return result;
} }
// Select track (so we can use it to listen to timed meta data updates)
private void selectTimedMetadataTrack(MediaPlayer mp) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
try { // It's possible this could throw an exception if the framework doesn't support getting track info
MediaPlayer.TrackInfo[] trackInfo = mp.getTrackInfo();
for (int i = 0; i < trackInfo.length; ++i) {
if (trackInfo[i].getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
mp.selectTrack(i);
break;
}
}
} catch (Exception e) {}
}
} }