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

Feat/ontimedmetadata android #707

Merged
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
57 changes: 55 additions & 2 deletions android/src/main/java/com/brentvatne/react/ReactVideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.res.AssetFileDescriptor;
import android.graphics.Matrix;
import android.media.MediaPlayer;
import android.media.TimedMetaData;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
Expand All @@ -16,6 +17,8 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.yqritc.scalablevideoview.ScalableType;
Expand All @@ -24,19 +27,27 @@
import com.yqritc.scalablevideoview.Size;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.lang.Math;

@SuppressLint("ViewConstructor")
public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer
.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener, LifecycleEventListener, MediaController.MediaPlayerControl {
public class ReactVideoView extends ScalableVideoView implements
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnCompletionListener,
MediaPlayer.OnInfoListener,
LifecycleEventListener,
MediaController.MediaPlayerControl {

public enum Events {
EVENT_LOAD_START("onVideoLoadStart"),
EVENT_LOAD("onVideoLoad"),
EVENT_ERROR("onVideoError"),
EVENT_PROGRESS("onVideoProgress"),
EVENT_TIMED_METADATA("onTimedMetadata"),
EVENT_SEEK("onVideoSeek"),
EVENT_END("onVideoEnd"),
EVENT_STALLED("onPlaybackStalled"),
Expand Down Expand Up @@ -70,6 +81,10 @@ public String toString() {
public static final String EVENT_PROP_WIDTH = "width";
public static final String EVENT_PROP_HEIGHT = "height";
public static final String EVENT_PROP_ORIENTATION = "orientation";
public static final String EVENT_PROP_METADATA = "metadata";
public static final String EVENT_PROP_TARGET = "target";
public static final String EVENT_PROP_METADATA_IDENTIFIER = "identifier";
public static final String EVENT_PROP_METADATA_VALUE = "value";

public static final String EVENT_PROP_ERROR = "error";
public static final String EVENT_PROP_WHAT = "what";
Expand Down Expand Up @@ -182,6 +197,9 @@ private void initializeMediaPlayerIfNeeded() {
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnInfoListener(this);
if (Build.VERSION.SDK_INT >= 23) {
mMediaPlayer.setOnTimedMetaDataAvailableListener(new TimedMetaDataAvailableListener());
}
}
}

Expand Down Expand Up @@ -444,6 +462,9 @@ public void run() {
}
});
}

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

@Override
Expand Down Expand Up @@ -478,6 +499,9 @@ 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);

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

Expand Down Expand Up @@ -528,6 +552,35 @@ public void onCompletion(MediaPlayer mp) {
isCompleted = true;
mEventEmitter.receiveEvent(getId(), Events.EVENT_END.toString(), null);
}

// This is not fully tested and does not work for all forms of timed metadata
@TargetApi(23) // 6.0
public class TimedMetaDataAvailableListener
implements MediaPlayer.OnTimedMetaDataAvailableListener
{
public void onTimedMetaDataAvailable(MediaPlayer mp, TimedMetaData data) {
WritableMap event = Arguments.createMap();

try {
String rawMeta = new String(data.getMetaData(), "UTF-8");
WritableMap id3 = Arguments.createMap();

id3.putString(EVENT_PROP_METADATA_VALUE, rawMeta.substring(rawMeta.lastIndexOf("\u0003") + 1));
id3.putString(EVENT_PROP_METADATA_IDENTIFIER, "id3/TDEN");

WritableArray metadata = new WritableNativeArray();

metadata.pushMap(id3);

event.putArray(EVENT_PROP_METADATA, metadata);
event.putDouble(EVENT_PROP_TARGET, getId());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

mEventEmitter.receiveEvent(getId(), Events.EVENT_TIMED_METADATA.toString(), event);
}
}

@Override
protected void onDetachedFromWindow() {
Expand Down