replaced channel with stereoPan attribute

This commit is contained in:
Rafael Pinto 2018-06-05 23:04:20 +01:00
parent 68dd2a72ba
commit 00fe87a2ab
6 changed files with 30 additions and 22 deletions

View File

@ -188,7 +188,7 @@ using System.Collections.Generic;
}} // Store reference }} // Store reference
rate={1.0} // 0 is paused, 1 is normal. rate={1.0} // 0 is paused, 1 is normal.
volume={1.0} // 0 is muted, 1 is normal. volume={1.0} // 0 is muted, 1 is normal.
channel="both" // [Android only] Target channel for audio panning: left, right or both (default). stereoPan={0} // [Android only] The audio players stereo pan position. A value of 1.0 is full left, 0.0 is center, and 1.0 is full right. Default 0.0
muted={true|false} // Mutes the audio entirely. Default false muted={true|false} // Mutes the audio entirely. Default false
paused={true|false} // Pauses playback entirely. Default false paused={true|false} // Pauses playback entirely. Default false
resizeMode="cover" // Fill the whole screen at aspect ratio.* resizeMode="cover" // Fill the whole screen at aspect ratio.*

View File

@ -277,7 +277,7 @@ Video.propTypes = {
paused: PropTypes.bool, paused: PropTypes.bool,
muted: PropTypes.bool, muted: PropTypes.bool,
volume: PropTypes.number, volume: PropTypes.number,
channel: PropTypes.string, stereoPan: PropTypes.number,
rate: PropTypes.number, rate: PropTypes.number,
playInBackground: PropTypes.bool, playInBackground: PropTypes.bool,
playWhenInactive: PropTypes.bool, playWhenInactive: PropTypes.bool,

View File

@ -650,9 +650,9 @@ class ReactExoplayerView extends FrameLayout implements
} }
} }
public void setChannel(String channel) { public void setStereoPan(float stereoPan) {
if (player != null) { if (player != null) {
player.setChannel(channel); player.setStereoPan(stereoPan);
} }
} }

View File

@ -132,9 +132,9 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
videoView.setVolumeModifier(volume); videoView.setVolumeModifier(volume);
} }
@ReactProp(name = PROP_CHANNEL) @ReactProp(name = PROP_STEREO_PAN)
public void setChannel(final ReactExoplayerView videoView, final String channel) { public void setStereoPan(final ReactExoplayerView videoView, final float stereoPan) {
videoView.setChannel(channel); videoView.setStereoPan(stereoPan);
} }
@ReactProp(name = PROP_PROGRESS_UPDATE_INTERVAL, defaultFloat = 250.0f) @ReactProp(name = PROP_PROGRESS_UPDATE_INTERVAL, defaultFloat = 250.0f)

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.lang.Math; import java.lang.Math;
import java.math.BigDecimal;
@SuppressLint("ViewConstructor") @SuppressLint("ViewConstructor")
public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer
@ -77,10 +78,6 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
public static final String EVENT_PROP_WHAT = "what"; public static final String EVENT_PROP_WHAT = "what";
public static final String EVENT_PROP_EXTRA = "extra"; public static final String EVENT_PROP_EXTRA = "extra";
public static final String CHANNEL_LEFT = "left";
public static final String CHANNEL_BOTH = "both";
public static final String CHANNEL_RIGHT = "right";
private ThemedReactContext mThemedReactContext; private ThemedReactContext mThemedReactContext;
private RCTEventEmitter mEventEmitter; private RCTEventEmitter mEventEmitter;
@ -99,7 +96,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
private boolean mPaused = false; private boolean mPaused = false;
private boolean mMuted = false; private boolean mMuted = false;
private float mVolume = 1.0f; private float mVolume = 1.0f;
private String mChannel = CHANNEL_BOTH; private float mStereoPan = 0.0f;
private float mProgressUpdateInterval = 250.0f; private float mProgressUpdateInterval = 250.0f;
private float mRate = 1.0f; private float mRate = 1.0f;
private float mActiveRate = 1.0f; private float mActiveRate = 1.0f;
@ -364,6 +361,14 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
} }
} }
// reduces the volume based on stereoPan
private float calulateRelativeVolume() {
float relativeVolume = (mVolume * (1 - Math.abs(mStereoPan)));
// only one decimal allowed
BigDecimal roundRelativeVolume = new BigDecimal(relativeVolume).setScale(1, BigDecimal.ROUND_HALF_UP);
return roundRelativeVolume.floatValue();
}
public void setMutedModifier(final boolean muted) { public void setMutedModifier(final boolean muted) {
mMuted = muted; mMuted = muted;
@ -373,11 +378,14 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
if (mMuted) { if (mMuted) {
setVolume(0, 0); setVolume(0, 0);
} else if (CHANNEL_LEFT.equals(mChannel)){ } else if (mStereoPan < 0) {
setVolume(mVolume, 0); // louder on the left channel
} else if (CHANNEL_RIGHT.equals(mChannel)){ setVolume(mVolume, calulateRelativeVolume());
setVolume(0, mVolume); } else if (mStereoPan > 0) {
// louder on the right channel
setVolume(calulateRelativeVolume(), mVolume);
} else { } else {
// same volume on both channels
setVolume(mVolume, mVolume); setVolume(mVolume, mVolume);
} }
} }
@ -387,8 +395,8 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
setMutedModifier(mMuted); setMutedModifier(mMuted);
} }
public void setChannel(final String channel) { public void setStereoPan(final float stereoPan) {
mChannel = channel; mStereoPan = stereoPan;
setMutedModifier(mMuted); setMutedModifier(mMuted);
} }

View File

@ -30,7 +30,7 @@ public class ReactVideoViewManager extends SimpleViewManager<ReactVideoView> {
public static final String PROP_PAUSED = "paused"; public static final String PROP_PAUSED = "paused";
public static final String PROP_MUTED = "muted"; public static final String PROP_MUTED = "muted";
public static final String PROP_VOLUME = "volume"; public static final String PROP_VOLUME = "volume";
public static final String PROP_CHANNEL = "channel"; public static final String PROP_STEREO_PAN = "stereoPan";
public static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval"; public static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval";
public static final String PROP_SEEK = "seek"; public static final String PROP_SEEK = "seek";
public static final String PROP_RATE = "rate"; public static final String PROP_RATE = "rate";
@ -125,9 +125,9 @@ public class ReactVideoViewManager extends SimpleViewManager<ReactVideoView> {
videoView.setVolumeModifier(volume); videoView.setVolumeModifier(volume);
} }
@ReactProp(name = PROP_CHANNEL) @ReactProp(name = PROP_STEREO_PAN)
public void setChannel(final ReactVideoView videoView, final String channel) { public void setStereoPan(final ReactVideoView videoView, final float stereoPan) {
videoView.setChannel(channel); videoView.setStereoPan(stereoPan);
} }
@ReactProp(name = PROP_PROGRESS_UPDATE_INTERVAL, defaultFloat = 250.0f) @ReactProp(name = PROP_PROGRESS_UPDATE_INTERVAL, defaultFloat = 250.0f)