Skip to content

Commit

Permalink
Merge pull request TheWidlarzGroup#1328 from linguokun1/master
Browse files Browse the repository at this point in the history
solve the memory leak on Android and avoid the crash on kitkat
  • Loading branch information
cobarx authored Dec 13, 2018
2 parents d5ee6b5 + b485e64 commit 52334f0
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions android/src/main/java/com/brentvatne/react/ReactVideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,19 @@ public void cleanupMediaPlayerResources() {
mediaController.hide();
}
if ( mMediaPlayer != null ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mMediaPlayer.setOnTimedMetaDataAvailableListener(null);
}
mMediaPlayerValid = false;
release();
}
if (mIsFullscreen) {
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) {
Expand Down Expand Up @@ -567,8 +574,7 @@ public void run() {
});
}

// Select track (so we can use it to listen to timed meta data updates)
mp.selectTrack(0);
selectTimedMetadataTrack(mp);
}

@Override
Expand Down Expand Up @@ -603,9 +609,7 @@ public boolean onInfo(MediaPlayer mp, int what, int extra) {

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Select track (so we can use it to listen to timed meta data updates)
mp.selectTrack(0);

selectTimedMetadataTrack(mp);
mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0);
}

Expand Down Expand Up @@ -761,4 +765,20 @@ public static Map<String, String> toStringMap(@Nullable ReadableMap readableMap)

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) {}
}
}

0 comments on commit 52334f0

Please sign in to comment.