[Android] Send onVideoLoadStart, onVideoLoad, and onVideoEnd events

This commit is contained in:
Baris Sencan 2015-11-08 18:19:03 -08:00
parent c03c45a362
commit 86790349e3

View File

@ -2,22 +2,23 @@ package com.brentvatne.react;
import android.content.Context; import android.content.Context;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.view.Gravity; import com.facebook.react.bridge.Arguments;
import android.widget.FrameLayout;
import android.support.annotation.Nullable;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder; import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ReactProp; import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.yqritc.scalablevideoview.ScalableType; import com.yqritc.scalablevideoview.ScalableType;
import com.yqritc.scalablevideoview.ScalableVideoView; import com.yqritc.scalablevideoview.ScalableVideoView;
import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> { public class ReactVideoViewManager extends SimpleViewManager<ScalableVideoView> {
public static final String REACT_CLASS = "RCTVideo"; public static final String REACT_CLASS = "RCTVideo";
private static final String PROP_SRC = "src"; private static final String PROP_SRC = "src";
@ -41,23 +42,18 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
} }
@Override @Override
protected FrameLayout createViewInstance(ThemedReactContext themedReactContext) { protected ScalableVideoView createViewInstance(ThemedReactContext themedReactContext) {
final FrameLayout container = new FrameLayout(themedReactContext); return new ScalableVideoView(themedReactContext);
final ScalableVideoView videoView = new ScalableVideoView(themedReactContext); }
final FrameLayout.LayoutParams videoLayout = new FrameLayout.LayoutParams( @Override
FrameLayout.LayoutParams.WRAP_CONTENT, @Nullable
FrameLayout.LayoutParams.WRAP_CONTENT public Map getExportedCustomDirectEventTypeConstants() {
); return MapBuilder.builder()
videoLayout.gravity = Gravity.CENTER; .put("onVideoLoadStart", MapBuilder.of("registrationName", "onVideoLoadStart"))
videoView.setLayoutParams(videoLayout); .put("onVideoLoad", MapBuilder.of("registrationName", "onVideoLoad"))
.put("onVideoEnd", MapBuilder.of("registrationName", "onVideoEnd"))
container.setLayoutParams(new FrameLayout.LayoutParams( .build();
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
));
container.addView(videoView);
return container;
} }
@Override @Override
@ -72,11 +68,13 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
} }
@ReactProp(name = PROP_SRC) @ReactProp(name = PROP_SRC)
public void setSrc(final FrameLayout container, @Nullable ReadableMap src) { public void setSrc(final ScalableVideoView videoView, @Nullable ReadableMap src) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0); final ThemedReactContext themedReactContext = (ThemedReactContext) videoView.getContext();
final RCTEventEmitter eventEmitter = themedReactContext.getJSModule(RCTEventEmitter.class);
try { try {
final String uriString = src.getString("uri"); final String uriString = src.getString("uri");
final String type = src.getString("type");
final boolean isNetwork = src.getBoolean("isNetwork"); final boolean isNetwork = src.getBoolean("isNetwork");
if (mPrepared) { if (mPrepared) {
@ -91,46 +89,72 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
videoView.setRawData(context.getResources().getIdentifier(uriString, "raw", context.getPackageName())); videoView.setRawData(context.getResources().getIdentifier(uriString, "raw", context.getPackageName()));
} }
WritableMap writableSrc = Arguments.createMap();
writableSrc.putString("uri", uriString);
writableSrc.putString("type", type);
writableSrc.putBoolean("isNetwork", isNetwork);
WritableMap event = Arguments.createMap();
event.putMap("src", writableSrc);
eventEmitter.receiveEvent(videoView.getId(), "onVideoLoadStart", event);
videoView.prepare(new MediaPlayer.OnPreparedListener() { videoView.prepare(new MediaPlayer.OnPreparedListener() {
@Override @Override
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
mPrepared = true; mPrepared = true;
applyModifiers(container);
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO: onVideoError
return false;
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
eventEmitter.receiveEvent(videoView.getId(), "onVideoEnd", null);
}
});
WritableMap event = Arguments.createMap();
event.putDouble("duration", (double) mp.getDuration() / (double) 1000);
event.putDouble("currentTime", (double) mp.getCurrentPosition() / (double) 1000);
// TODO: Add canX properties.
eventEmitter.receiveEvent(videoView.getId(), "onVideoLoad", event);
applyModifiers(videoView);
} }
}); });
} catch (Exception e) { } catch (Exception e) {
assertTrue("failed to set video source", false); // TODO: onVideoError
} }
} }
@ReactProp(name = PROP_RESIZE_MODE) @ReactProp(name = PROP_RESIZE_MODE)
public void setResizeMode(final FrameLayout container, final int resizeModeOrdinal) { public void setResizeMode(final ScalableVideoView videoView, final int resizeModeOrdinal) {
mResizeMode = ScalableType.values()[resizeModeOrdinal]; mResizeMode = ScalableType.values()[resizeModeOrdinal];
if (mPrepared) { if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.setScalableType(mResizeMode); videoView.setScalableType(mResizeMode);
videoView.invalidate(); videoView.invalidate();
} }
} }
@ReactProp(name = PROP_REPEAT) @ReactProp(name = PROP_REPEAT)
public void setRepeat(final FrameLayout container, final boolean repeat) { public void setRepeat(final ScalableVideoView videoView, final boolean repeat) {
mRepeat = repeat; mRepeat = repeat;
if (mPrepared) { if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.setLooping(mRepeat); videoView.setLooping(mRepeat);
} }
} }
@ReactProp(name = PROP_PAUSED) @ReactProp(name = PROP_PAUSED)
public void setPaused(final FrameLayout container, final boolean paused) { public void setPaused(final ScalableVideoView videoView, final boolean paused) {
mPaused = paused; mPaused = paused;
if (mPrepared) { if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.requestFocus();
if (!mPaused) { if (!mPaused) {
videoView.start(); videoView.start();
} else { } else {
@ -140,12 +164,10 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
} }
@ReactProp(name = PROP_MUTED) @ReactProp(name = PROP_MUTED)
public void setMuted(final FrameLayout container, final boolean muted) { public void setMuted(final ScalableVideoView videoView, final boolean muted) {
mMuted = muted; mMuted = muted;
if (mPrepared) { if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
if (mMuted) { if (mMuted) {
videoView.setVolume(0, 0); videoView.setVolume(0, 0);
} else { } else {
@ -155,19 +177,18 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
} }
@ReactProp(name = PROP_VOLUME) @ReactProp(name = PROP_VOLUME)
public void setVolume(final FrameLayout container, final float volume) { public void setVolume(final ScalableVideoView videoView, final float volume) {
mVolume = volume; mVolume = volume;
if (mPrepared) { if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.setVolume(mVolume, mVolume); videoView.setVolume(mVolume, mVolume);
} }
} }
private void applyModifiers(final FrameLayout container) { private void applyModifiers(final ScalableVideoView videoView) {
setResizeMode(container, mResizeMode.ordinal()); setResizeMode(videoView, mResizeMode.ordinal());
setRepeat(container, mRepeat); setRepeat(videoView, mRepeat);
setPaused(container, mPaused); setPaused(videoView, mPaused);
setMuted(container, mMuted); setMuted(videoView, mMuted);
} }
} }