react-native-video/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java

126 lines
4.9 KiB
Java
Raw Normal View History

2015-10-30 17:43:18 -07:00
package com.brentvatne.react;
import com.brentvatne.react.ReactVideoView.Events;
import com.facebook.react.bridge.Arguments;
2015-10-30 17:43:18 -07:00
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
2015-10-30 17:43:18 -07:00
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.yqritc.scalablevideoview.ScalableType;
2015-10-30 17:43:18 -07:00
import javax.annotation.Nullable;
import java.util.Map;
2015-11-09 17:54:15 -08:00
public class ReactVideoViewManager extends SimpleViewManager<ReactVideoView> {
2015-10-30 17:43:18 -07:00
public static final String REACT_CLASS = "RCTVideo";
2015-11-09 17:54:15 -08:00
public static final String PROP_SRC = "src";
public static final String PROP_SRC_URI = "uri";
public static final String PROP_SRC_TYPE = "type";
public static final String PROP_SRC_IS_NETWORK = "isNetwork";
public static final String PROP_RESIZE_MODE = "resizeMode";
public static final String PROP_REPEAT = "repeat";
public static final String PROP_PAUSED = "paused";
public static final String PROP_MUTED = "muted";
public static final String PROP_VOLUME = "volume";
public static final String PROP_SEEK = "seek";
public static final String PROP_RATE = "rate";
2015-11-09 17:54:15 -08:00
2015-10-30 17:43:18 -07:00
@Override
public String getName() {
return REACT_CLASS;
}
@Override
2015-11-09 17:54:15 -08:00
protected ReactVideoView createViewInstance(ThemedReactContext themedReactContext) {
return new ReactVideoView(themedReactContext);
}
@Override
@Nullable
public Map getExportedCustomDirectEventTypeConstants() {
MapBuilder.Builder builder = MapBuilder.builder();
for (Events event : Events.values()) {
builder.put(event.toString(), MapBuilder.of("registrationName", event.toString()));
}
return builder.build();
}
@Override
@Nullable
public Map getExportedViewConstants() {
return MapBuilder.of(
2015-11-09 17:54:15 -08:00
"ScaleNone", Integer.toString(ScalableType.LEFT_TOP.ordinal()),
"ScaleToFill", Integer.toString(ScalableType.FIT_XY.ordinal()),
"ScaleAspectFit", Integer.toString(ScalableType.FIT_CENTER.ordinal()),
"ScaleAspectFill", Integer.toString(ScalableType.CENTER_CROP.ordinal())
);
2015-10-30 17:43:18 -07:00
}
@ReactProp(name = PROP_SRC)
2015-11-09 17:54:15 -08:00
public void setSrc(final ReactVideoView videoView, @Nullable ReadableMap src) {
final ThemedReactContext themedReactContext = (ThemedReactContext) videoView.getContext();
final RCTEventEmitter eventEmitter = themedReactContext.getJSModule(RCTEventEmitter.class);
try {
2015-11-09 17:54:15 -08:00
final String uriString = src.getString(PROP_SRC_URI);
final String type = src.getString(PROP_SRC_TYPE);
final boolean isNetwork = src.getBoolean(PROP_SRC_IS_NETWORK);
2015-11-12 19:27:12 -08:00
// TODO: Carry this inside videoView.setSrc
WritableMap writableSrc = Arguments.createMap();
2015-11-09 17:54:15 -08:00
writableSrc.putString(PROP_SRC_URI, uriString);
writableSrc.putString(PROP_SRC_TYPE, type);
writableSrc.putBoolean(PROP_SRC_IS_NETWORK, isNetwork);
WritableMap event = Arguments.createMap();
2015-11-09 17:54:15 -08:00
event.putMap(PROP_SRC, writableSrc);
eventEmitter.receiveEvent(videoView.getId(), Events.EVENT_LOAD_START.toString(), event);
2015-11-12 19:27:12 -08:00
videoView.setSrc(uriString, isNetwork);
} catch (Exception e) {
2015-11-12 19:27:12 -08:00
// TODO: Send onError
// TODO: Carry inside videoView.setSrc
e.printStackTrace();
}
}
@ReactProp(name = PROP_RESIZE_MODE)
2015-11-09 17:54:15 -08:00
public void setResizeMode(final ReactVideoView videoView, final String resizeModeOrdinalString) {
videoView.setResizeModeModifier(ScalableType.values()[Integer.parseInt(resizeModeOrdinalString)]);
}
2015-11-13 14:36:15 -08:00
@ReactProp(name = PROP_REPEAT, defaultBoolean = false)
2015-11-09 17:54:15 -08:00
public void setRepeat(final ReactVideoView videoView, final boolean repeat) {
videoView.setRepeatModifier(repeat);
}
2015-11-13 14:36:15 -08:00
@ReactProp(name = PROP_PAUSED, defaultBoolean = false)
2015-11-09 17:54:15 -08:00
public void setPaused(final ReactVideoView videoView, final boolean paused) {
videoView.setPausedModifier(paused);
}
2015-11-13 14:36:15 -08:00
@ReactProp(name = PROP_MUTED, defaultBoolean = false)
2015-11-09 17:54:15 -08:00
public void setMuted(final ReactVideoView videoView, final boolean muted) {
videoView.setMutedModifier(muted);
}
2015-11-13 14:36:15 -08:00
@ReactProp(name = PROP_VOLUME, defaultFloat = 1.0f)
2015-11-09 17:54:15 -08:00
public void setVolume(final ReactVideoView videoView, final float volume) {
videoView.setVolumeModifier(volume);
2015-10-30 17:43:18 -07:00
}
@ReactProp(name = PROP_SEEK)
public void setSeek(final ReactVideoView videoView, final float seek) {
videoView.seekTo(Math.round(seek * 1000.0f));
}
@ReactProp(name = PROP_RATE)
public void setRate(final ReactVideoView videoView, final float rate) {
videoView.setRateModifier(rate);
}
2015-10-30 17:43:18 -07:00
}