[Android] repeat, muted, volume properties fully working

This commit is contained in:
Baris Sencan
2015-11-05 17:13:35 -08:00
parent 4362e596a8
commit c03c45a362
4 changed files with 116 additions and 105 deletions

View File

@@ -1,17 +1,17 @@
package com.brentvatne.react;
import android.graphics.Color;
import android.content.Context;
import android.media.MediaPlayer;
import android.widget.MediaController;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.VideoView;
import android.support.annotation.Nullable;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.yqritc.scalablevideoview.ScalableType;
import com.yqritc.scalablevideoview.ScalableVideoView;
import java.util.Map;
@@ -24,13 +24,16 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
private static final String PROP_RESIZE_MODE = "resizeMode";
private static final String PROP_REPEAT = "repeat";
private static final String PROP_PAUSED = "paused";
private static final String PROP_MUTED = "muted";
private static final String PROP_VOLUME = "volume";
private enum ResizeMode {
SCALE_NONE, SCALE_TO_FILL, SCALE_ASPECT_FIT, SCALE_ASPECT_FILL
}
private boolean mPrepared = false;
private ScalableType mResizeMode = ScalableType.LEFT_TOP;
private boolean mRepeat = false;
private boolean mPaused = false;
private boolean mMuted = false;
private float mVolume = 1;
@Override
public String getName() {
@@ -40,93 +43,74 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
@Override
protected FrameLayout createViewInstance(ThemedReactContext themedReactContext) {
final FrameLayout container = new FrameLayout(themedReactContext);
final VideoView videoView = new VideoView(themedReactContext);
final ScalableVideoView videoView = new ScalableVideoView(themedReactContext);
MediaController mediaController = new MediaController(themedReactContext);
mediaController.setAnchorView(videoView);
mediaController.setVisibility(VideoView.GONE);
videoView.setMediaController(mediaController);
videoView.setLayoutParams(new FrameLayout.LayoutParams(
final FrameLayout.LayoutParams videoLayout = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
videoLayout.gravity = Gravity.CENTER;
videoView.setLayoutParams(videoLayout);
container.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
FrameLayout.LayoutParams.MATCH_PARENT
));
container.addView(videoView);
return container;
}
@Override
public
@Nullable
Map getExportedViewConstants() {
public Map getExportedViewConstants() {
return MapBuilder.of(
"ScaleNone", ResizeMode.SCALE_NONE.ordinal(),
"ScaleToFill", ResizeMode.SCALE_TO_FILL.ordinal(),
"ScaleAspectFit", ResizeMode.SCALE_ASPECT_FIT.ordinal(),
"ScaleAspectFill", ResizeMode.SCALE_ASPECT_FILL.ordinal());
"ScaleNone", ScalableType.LEFT_TOP.ordinal(),
"ScaleToFill", ScalableType.FIT_XY.ordinal(),
"ScaleAspectFit", ScalableType.FIT_CENTER.ordinal(),
"ScaleAspectFill", ScalableType.CENTER_CROP.ordinal()
);
}
@ReactProp(name = PROP_SRC)
public void setSrc(final FrameLayout container, @Nullable ReadableMap src) {
final VideoView videoView = (VideoView) container.getChildAt(0);
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
try {
final String uriString = src.getString("uri");
final boolean isNetwork = src.getBoolean("isNetwork");
videoView.stopPlayback();
if (mPrepared) {
videoView.stop();
mPrepared = false;
}
if (isNetwork) {
videoView.setVideoPath(uriString);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
applyModifiers(container);
}
});
videoView.setDataSource(uriString);
} else {
videoView.setVideoURI(Uri.parse("android.resource://" + videoView.getContext().getPackageName() +
"/raw/" + uriString));
applyModifiers(container);
Context context = videoView.getContext();
videoView.setRawData(context.getResources().getIdentifier(uriString, "raw", context.getPackageName()));
}
videoView.prepare(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mPrepared = true;
applyModifiers(container);
}
});
} catch (Exception e) {
assertTrue("failed to set video source", false);
}
}
@ReactProp(name = PROP_RESIZE_MODE)
public void setResizeMode(final FrameLayout container, int resizeModeOrdinal) {
final VideoView videoView = (VideoView) container.getChildAt(0);
public void setResizeMode(final FrameLayout container, final int resizeModeOrdinal) {
mResizeMode = ScalableType.values()[resizeModeOrdinal];
try {
final ResizeMode resizeMode = ResizeMode.values()[resizeModeOrdinal];
FrameLayout.LayoutParams layoutParams = null;
switch (resizeMode) {
case SCALE_NONE:
layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
break;
case SCALE_TO_FILL:
layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
break;
case SCALE_ASPECT_FIT:
break;
case SCALE_ASPECT_FILL:
break;
}
if (layoutParams != null) {
videoView.setLayoutParams(layoutParams);
container.updateViewLayout(videoView, layoutParams);
}
} catch (Exception e) {
assertTrue("failed to set video resize mode", false);
if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.setScalableType(mResizeMode);
videoView.invalidate();
}
}
@@ -134,35 +118,56 @@ public class ReactVideoViewManager extends SimpleViewManager<FrameLayout> {
public void setRepeat(final FrameLayout container, final boolean repeat) {
mRepeat = repeat;
if (!mRepeat) { return; }
final VideoView videoView = (VideoView) container.getChildAt(0);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mRepeat) {
mp.seekTo(0);
setPaused(container, mPaused);
}
}
});
if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.setLooping(mRepeat);
}
}
@ReactProp(name = PROP_PAUSED)
public void setPaused(final FrameLayout container, final boolean paused) {
final VideoView videoView = (VideoView) container.getChildAt(0);
videoView.requestFocus();
if (!paused) {
videoView.start();
} else {
videoView.pause();
}
mPaused = paused;
if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.requestFocus();
if (!mPaused) {
videoView.start();
} else {
videoView.pause();
}
}
}
@ReactProp(name = PROP_MUTED)
public void setMuted(final FrameLayout container, final boolean muted) {
mMuted = muted;
if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
if (mMuted) {
videoView.setVolume(0, 0);
} else {
videoView.setVolume(mVolume, mVolume);
}
}
}
@ReactProp(name = PROP_VOLUME)
public void setVolume(final FrameLayout container, final float volume) {
mVolume = volume;
if (mPrepared) {
final ScalableVideoView videoView = (ScalableVideoView) container.getChildAt(0);
videoView.setVolume(mVolume, mVolume);
}
}
private void applyModifiers(final FrameLayout container) {
setResizeMode(container, mResizeMode.ordinal());
setRepeat(container, mRepeat);
setPaused(container, mPaused);
setMuted(container, mMuted);
}
}