Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solve the memory leak on Android and avoid the crash on kitkat #1328

Merged
merged 3 commits into from
Dec 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -232,12 +232,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 @@ -564,8 +571,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 @@ -600,9 +606,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 @@ -755,4 +759,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) {}
}
}