Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added the full screen functionality to the android Exoplayer control
  • Loading branch information
Ibrahim Sulaiman committed Aug 27, 2019
commit 2cb4b4dd4fab918a13f2fefece14250a9afdd19f
78 changes: 46 additions & 32 deletions Video.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {Component} from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {StyleSheet, requireNativeComponent, NativeModules, View, ViewPropTypes, Image, Platform, findNodeHandle} from 'react-native';
import { StyleSheet, requireNativeComponent, NativeModules, View, ViewPropTypes, Image, Platform, findNodeHandle, Dimensions } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import TextTrackType from './TextTrackType';
import FilterType from './FilterType';
Expand All @@ -20,24 +20,25 @@ export default class Video extends Component {
super(props);

this.state = {
showPoster: !!props.poster
showPoster: !!props.poster,
androidFullScreen: false
};
}

setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps);
}

toTypeString(x) {
switch (typeof x) {
case "object":
return x instanceof Date
? x.toISOString()
return x instanceof Date
? x.toISOString()
: JSON.stringify(x); // object, null
case "undefined":
return "";
default: // boolean, number, string
return x.toString();
return x.toString();
}
}

Expand All @@ -53,7 +54,7 @@ export default class Video extends Component {

seek = (time, tolerance = 100) => {
if (isNaN(time)) throw new Error('Specified time is not a number');

if (Platform.OS === 'ios') {
this.setNativeProps({
seek: {
Expand Down Expand Up @@ -88,7 +89,7 @@ export default class Video extends Component {

_hidePoster = () => {
if (this.state.showPoster) {
this.setState({showPoster: false});
this.setState({ showPoster: false });
}
}

Expand Down Expand Up @@ -124,7 +125,7 @@ export default class Video extends Component {
if (this.props.onBandwidthUpdate) {
this.props.onBandwidthUpdate(event.nativeEvent);
}
};
};

_onSeek = (event) => {
if (this.props.onSeek) {
Expand All @@ -145,6 +146,7 @@ export default class Video extends Component {
};

_onFullscreenPlayerWillPresent = (event) => {
Platform.OS === 'android' && this.setState({ androidFullScreen: true })
if (this.props.onFullscreenPlayerWillPresent) {
this.props.onFullscreenPlayerWillPresent(event.nativeEvent);
}
Expand All @@ -157,6 +159,7 @@ export default class Video extends Component {
};

_onFullscreenPlayerWillDismiss = (event) => {
Platform.OS === 'android' && this.setState({ androidFullScreen: false })
if (this.props.onFullscreenPlayerWillDismiss) {
this.props.onFullscreenPlayerWillDismiss(event.nativeEvent);
}
Expand Down Expand Up @@ -192,7 +195,7 @@ export default class Video extends Component {
this.props.onPlaybackRateChange(event.nativeEvent);
}
};

_onExternalPlaybackChange = (event) => {
if (this.props.onExternalPlaybackChange) {
this.props.onExternalPlaybackChange(event.nativeEvent);
Expand All @@ -212,7 +215,7 @@ export default class Video extends Component {
};

_onRestoreUserInterfaceForPictureInPictureStop = (event) => {
if (this.props.onRestoreUserInterfaceForPictureInPictureStop) {
if (this.props.onRestoreUserInterfaceForPictureInPictureStop) {
this.props.onRestoreUserInterfaceForPictureInPictureStop();
}
};
Expand Down Expand Up @@ -245,7 +248,7 @@ export default class Video extends Component {
if (uri && uri.match(/^\//)) {
uri = `file://${uri}`;
}

if (!uri) {
console.warn('Trying to load empty source.');
}
Expand All @@ -267,8 +270,19 @@ export default class Video extends Component {
}

const nativeProps = Object.assign({}, this.props);
//androidFullScreen property will only impact on android. It will be always false for iOS.
const videoStyle = this.state.androidFullScreen ? {
position: 'absolute',
top: 0,
left: 0,
width: Math.round(Dimensions.get('window').width),
height: Math.round(Dimensions.get('window').height),
backgroundColor: 'transparent',
justifyContent: "center",
zIndex: 99999
} : {}
Object.assign(nativeProps, {
style: [styles.base, nativeProps.style],
style: [styles.base, nativeProps.style, videoStyle],
resizeMode: nativeResizeMode,
src: {
uri,
Expand Down Expand Up @@ -327,22 +341,22 @@ export default class Video extends Component {

Video.propTypes = {
filter: PropTypes.oneOf([
FilterType.NONE,
FilterType.INVERT,
FilterType.MONOCHROME,
FilterType.POSTERIZE,
FilterType.FALSE,
FilterType.MAXIMUMCOMPONENT,
FilterType.MINIMUMCOMPONENT,
FilterType.CHROME,
FilterType.FADE,
FilterType.INSTANT,
FilterType.MONO,
FilterType.NOIR,
FilterType.PROCESS,
FilterType.TONAL,
FilterType.TRANSFER,
FilterType.SEPIA
FilterType.NONE,
FilterType.INVERT,
FilterType.MONOCHROME,
FilterType.POSTERIZE,
FilterType.FALSE,
FilterType.MAXIMUMCOMPONENT,
FilterType.MINIMUMCOMPONENT,
FilterType.CHROME,
FilterType.FADE,
FilterType.INSTANT,
FilterType.MONO,
FilterType.NOIR,
FilterType.PROCESS,
FilterType.TONAL,
FilterType.TRANSFER,
FilterType.SEPIA
]),
filterEnabled: PropTypes.bool,
/* Native only */
Expand Down Expand Up @@ -396,7 +410,7 @@ Video.propTypes = {
PropTypes.string,
PropTypes.number
])
}),
}),
selectedTextTrack: PropTypes.shape({
type: PropTypes.string.isRequired,
value: PropTypes.oneOfType([
Expand Down Expand Up @@ -437,7 +451,7 @@ Video.propTypes = {
audioOnly: PropTypes.bool,
currentTime: PropTypes.number,
fullscreenAutorotate: PropTypes.bool,
fullscreenOrientation: PropTypes.oneOf(['all','landscape','portrait']),
fullscreenOrientation: PropTypes.oneOf(['all', 'landscape', 'portrait']),
progressUpdateInterval: PropTypes.number,
useTextureView: PropTypes.bool,
hideShutterView: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
Expand All @@ -13,6 +14,7 @@
import android.view.Window;
import android.view.accessibility.CaptioningManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.brentvatne.react.R;
import com.brentvatne.receiver.AudioBecomingNoisyReceiver;
Expand Down Expand Up @@ -298,6 +300,17 @@ public void onClick(View v) {
}
});

//Handling the fullScreenButton click event
FrameLayout fullScreenButton = playerControlView.findViewById(R.id.exo_fullscreen_button);
fullScreenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Play the video whenever the user clicks minimize or maximise button. In order to enable the controls
player.setPlayWhenReady(true);
updateFullScreenIcon(isFullscreen);
}
});

// Invoking onPlayerStateChanged event for Player
eventListener = new Player.EventListener() {
@Override
Expand Down Expand Up @@ -326,6 +339,32 @@ private void addPlayerControl() {
addView(playerControlView, 1, layoutParams);
}

/**
* Update fullscreen icon
*/
private void updateFullScreenIcon(Boolean fullScreen) {
if(playerControlView != null) {
ImageView fullScreenIcon = playerControlView.findViewById(R.id.exo_fullscreen_icon);
if (isFullscreen) {
fullScreenIcon.setImageResource(R.drawable.fullscreen_expand);
} else {
fullScreenIcon.setImageResource(R.drawable.fullscreen_shrink);
}
setFullscreen(!isFullscreen);
}
}

/**
* Enable or Disable fullscreen button
*/
private void enableFullScreenButton(Boolean enable) {
if(playerControlView != null) {
FrameLayout fullScreenButton = playerControlView.findViewById(R.id.exo_fullscreen_button);
fullScreenButton.setAlpha(enable ? 1.0f : 0.5f);
fullScreenButton.setEnabled(enable);
}
}

/**
* Update the layout
* @param view view needs to update layout
Expand Down Expand Up @@ -530,10 +569,13 @@ private void stopPlayback() {

private void onStopPlayback() {
if (isFullscreen) {
setFullscreen(false);
//When the video stopPlayback.
//If the video is in fullscreen, then we will update the video to normal mode.
updateFullScreenIcon(isFullscreen);
}
setKeepScreenOn(false);
audioManager.abandonAudioFocus(this);
enableFullScreenButton(false);
}

private void updateResumePosition() {
Expand Down Expand Up @@ -620,6 +662,7 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if(playerControlView != null) {
playerControlView.show();
}
enableFullScreenButton(true);
break;
case ExoPlayer.STATE_ENDED:
text += "ended";
Expand Down Expand Up @@ -1143,11 +1186,13 @@ public void setFullscreen(boolean fullscreen) {
uiOptions = SYSTEM_UI_FLAG_HIDE_NAVIGATION
| SYSTEM_UI_FLAG_FULLSCREEN;
}
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
eventEmitter.fullscreenWillPresent();
decorView.setSystemUiVisibility(uiOptions);
eventEmitter.fullscreenDidPresent();
} else {
uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
eventEmitter.fullscreenWillDismiss();
decorView.setSystemUiVisibility(uiOptions);
eventEmitter.fullscreenDidDismiss();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions android-exoplayer/src/main/res/layout/exo_player_control_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@
android:paddingRight="4dp"
android:includeFontPadding="false"
android:textColor="#FFBEBEBE"/>
<FrameLayout
android:id="@+id/exo_fullscreen_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="right">

<ImageView
android:id="@+id/exo_fullscreen_icon"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/fullscreen_expand"/>

</FrameLayout>
</LinearLayout>

</LinearLayout>