From 6184c10acc90defd63cd55af51458864dfe112d5 Mon Sep 17 00:00:00 2001 From: coofzilla <75731066+coofzilla@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:50:19 +0900 Subject: [PATCH] feat(android): add subtitle event (#3566) * feat: add onCues method to get access to subtitle content --- .../brentvatne/common/react/VideoEventEmitter.java | 11 +++++++++++ .../com/brentvatne/exoplayer/ReactExoplayerView.java | 8 ++++++++ docs/pages/component/events.mdx | 2 +- examples/basic/src/VideoPlayer.tsx | 5 ++--- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java b/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java index ed194950..d87b6d84 100644 --- a/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java +++ b/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java @@ -59,6 +59,8 @@ public class VideoEventEmitter { private static final String EVENT_VOLUME_CHANGE = "onVolumeChange"; private static final String EVENT_AUDIO_TRACKS = "onAudioTracks"; private static final String EVENT_TEXT_TRACKS = "onTextTracks"; + + private static final String EVENT_TEXT_TRACK_DATA_CHANGED = "onTextTrackDataChanged"; private static final String EVENT_VIDEO_TRACKS = "onVideoTracks"; private static final String EVENT_ON_RECEIVE_AD_EVENT = "onReceiveAdEvent"; @@ -86,6 +88,7 @@ public class VideoEventEmitter { EVENT_VOLUME_CHANGE, EVENT_AUDIO_TRACKS, EVENT_TEXT_TRACKS, + EVENT_TEXT_TRACK_DATA_CHANGED, EVENT_VIDEO_TRACKS, EVENT_BANDWIDTH, EVENT_ON_RECEIVE_AD_EVENT @@ -116,6 +119,7 @@ public class VideoEventEmitter { EVENT_VOLUME_CHANGE, EVENT_AUDIO_TRACKS, EVENT_TEXT_TRACKS, + EVENT_TEXT_TRACK_DATA_CHANGED, EVENT_VIDEO_TRACKS, EVENT_BANDWIDTH, EVENT_ON_RECEIVE_AD_EVENT @@ -144,6 +148,7 @@ public class VideoEventEmitter { private static final String EVENT_PROP_VIDEO_TRACKS = "videoTracks"; private static final String EVENT_PROP_AUDIO_TRACKS = "audioTracks"; private static final String EVENT_PROP_TEXT_TRACKS = "textTracks"; + private static final String EVENT_PROP_TEXT_TRACK_DATA = "subtitleTracks"; private static final String EVENT_PROP_HAS_AUDIO_FOCUS = "hasAudioFocus"; private static final String EVENT_PROP_IS_BUFFERING = "isBuffering"; private static final String EVENT_PROP_PLAYBACK_RATE = "playbackRate"; @@ -284,6 +289,12 @@ public class VideoEventEmitter { receiveEvent(EVENT_TEXT_TRACKS, arrayToObject(EVENT_PROP_TEXT_TRACKS, textTracksToArray(textTracks))); } + public void textTrackDataChanged(String textTrackData){ + WritableMap event = Arguments.createMap(); + event.putString(EVENT_PROP_TEXT_TRACK_DATA, textTrackData); + receiveEvent(EVENT_TEXT_TRACK_DATA_CHANGED, event); + } + public void videoTracks(ArrayList videoTracks){ receiveEvent(EVENT_VIDEO_TRACKS, arrayToObject(EVENT_PROP_VIDEO_TRACKS, videoTracksToArray(videoTracks))); } diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index d67493c0..5036d439 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -41,6 +41,7 @@ import androidx.media3.common.Timeline; import androidx.media3.common.TrackGroup; import androidx.media3.common.TrackSelectionOverride; import androidx.media3.common.Tracks; +import androidx.media3.common.text.CueGroup; import androidx.media3.common.util.Util; import androidx.media3.datasource.DataSource; import androidx.media3.datasource.DataSpec; @@ -1521,6 +1522,13 @@ public class ReactExoplayerView extends FrameLayout implements eventEmitter.timedMetadata(metadataArray); } + public void onCues(CueGroup cueGroup) { + if (!cueGroup.cues.isEmpty() && cueGroup.cues.get(0).text != null) { + String subtitleText = cueGroup.cues.get(0).text.toString(); + eventEmitter.textTrackDataChanged(subtitleText); + } + } + // ReactExoplayerViewManager public api public void setSrc(final Uri uri, final long startPositionMs, final long cropStartMs, final long cropEndMs, final String extension, Map headers) { diff --git a/docs/pages/component/events.mdx b/docs/pages/component/events.mdx index 462e6eba..fab327f8 100644 --- a/docs/pages/component/events.mdx +++ b/docs/pages/component/events.mdx @@ -516,7 +516,7 @@ Example: ### `onTextTrackDataChanged` - + Callback function that is called when new subtitle data is available. It provides the actual subtitle content for the current selected text track, if available (mainly WebVTT). diff --git a/examples/basic/src/VideoPlayer.tsx b/examples/basic/src/VideoPlayer.tsx index ed9f06f5..fd87bf57 100644 --- a/examples/basic/src/VideoPlayer.tsx +++ b/examples/basic/src/VideoPlayer.tsx @@ -120,15 +120,14 @@ class VideoPlayer extends Component { description: 'another bunny (can be saved)', uri: 'https://rawgit.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4', }, - ]; - - srcIosList = [ { description: 'sintel with subtitles', uri: 'https://bitmovin-a.akamaihd.net/content/sintel/hls/playlist.m3u8', }, ]; + srcIosList = []; + srcAndroidList = [ { description: 'Another live sample',