add support for onTimedMetaData for android

This commit is contained in:
RWOverdijk 2017-07-25 16:41:20 +02:00
parent b303f991ce
commit 38d225294f

View File

@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.content.res.AssetFileDescriptor; import android.content.res.AssetFileDescriptor;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.media.TimedMetaData;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
@ -16,6 +17,8 @@ import com.android.vending.expansion.zipfile.ZipResourceFile;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.WritableMap; 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.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter; import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.yqritc.scalablevideoview.ScalableType; import com.yqritc.scalablevideoview.ScalableType;
@ -24,19 +27,28 @@ import com.yqritc.scalablevideoview.ScaleManager;
import com.yqritc.scalablevideoview.Size; import com.yqritc.scalablevideoview.Size;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.lang.Math; import java.lang.Math;
@SuppressLint("ViewConstructor") @SuppressLint("ViewConstructor")
public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer public class ReactVideoView extends ScalableVideoView implements
.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener, LifecycleEventListener, MediaController.MediaPlayerControl { MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnTimedMetaDataAvailableListener,
MediaPlayer.OnCompletionListener,
MediaPlayer.OnInfoListener,
LifecycleEventListener,
MediaController.MediaPlayerControl {
public enum Events { public enum Events {
EVENT_LOAD_START("onVideoLoadStart"), EVENT_LOAD_START("onVideoLoadStart"),
EVENT_LOAD("onVideoLoad"), EVENT_LOAD("onVideoLoad"),
EVENT_ERROR("onVideoError"), EVENT_ERROR("onVideoError"),
EVENT_PROGRESS("onVideoProgress"), EVENT_PROGRESS("onVideoProgress"),
EVENT_TIMED_METADATA("onTimedMetadata"),
EVENT_SEEK("onVideoSeek"), EVENT_SEEK("onVideoSeek"),
EVENT_END("onVideoEnd"), EVENT_END("onVideoEnd"),
EVENT_STALLED("onPlaybackStalled"), EVENT_STALLED("onPlaybackStalled"),
@ -70,6 +82,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
public static final String EVENT_PROP_WIDTH = "width"; public static final String EVENT_PROP_WIDTH = "width";
public static final String EVENT_PROP_HEIGHT = "height"; public static final String EVENT_PROP_HEIGHT = "height";
public static final String EVENT_PROP_ORIENTATION = "orientation"; public static final String EVENT_PROP_ORIENTATION = "orientation";
public static final String EVENT_PROP_METADATA = "metadata";
public static final String EVENT_PROP_ERROR = "error"; public static final String EVENT_PROP_ERROR = "error";
public static final String EVENT_PROP_WHAT = "what"; public static final String EVENT_PROP_WHAT = "what";
@ -181,6 +194,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnTimedMetaDataAvailableListener(this);
mMediaPlayer.setOnInfoListener(this); mMediaPlayer.setOnInfoListener(this);
} }
} }
@ -444,6 +458,9 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
} }
}); });
} }
// Select track (so we can use it to listen to timed meta data updates)
mp.selectTrack(0);
} }
@Override @Override
@ -478,6 +495,9 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
@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)
mp.selectTrack(0);
mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0); mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0);
} }
@ -529,6 +549,30 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
mEventEmitter.receiveEvent(getId(), Events.EVENT_END.toString(), null); mEventEmitter.receiveEvent(getId(), Events.EVENT_END.toString(), null);
} }
@Override
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("value", rawMeta.substring(rawMeta.lastIndexOf("\u0003") + 1));
id3.putString("identifier", "id3/TDEN");
WritableArray metadata = new WritableNativeArray();
metadata.pushMap(id3);
event.putArray(EVENT_PROP_METADATA, metadata);
event.putDouble("target", getId());
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
mEventEmitter.receiveEvent(getId(), Events.EVENT_TIMED_METADATA.toString(), event);
}
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {