import * as React from 'react';
import { Component } from 'react';
import PlayIcon from '@atlaskit/icon/glyph/vid-play';
import PauseIcon from '@atlaskit/icon/glyph/vid-pause';
import FullScreenIconOn from '@atlaskit/icon/glyph/vid-full-screen-on';
import FullScreenIconOff from '@atlaskit/icon/glyph/vid-full-screen-off';
import SoundIcon from '@atlaskit/icon/glyph/hipchat/outgoing-sound';
import HDIcon from '@atlaskit/icon/glyph/vid-hd-circle';
import Button from '@uidu/button';
import Spinner from '@uidu/spinner';
import MediaPlayer, {
SetVolumeFunction,
NavigateFunction,
VideoState,
VideoActions,
} from 'react-video-renderer';
import { colors } from '@uidu/theme';
import { ThemeProvider } from 'styled-components';
import theme from '../theme';
import { TimeRange } from './timeRange';
import {
CurrentTime,
VideoWrapper,
CustomVideoWrapper,
TimebarWrapper,
VolumeWrapper,
TimeWrapper,
LeftControls,
RightControls,
ControlsWrapper,
VolumeToggleWrapper,
MutedIndicator,
SpinnerWrapper,
VolumeTimeRangeWrapper,
} from './styled';
import { formatDuration } from '../formatDuration';
import { hideControlsClassName } from '../classNames';
import { Shortcut, keyCodes } from '../shortcut';
import {
toggleFullscreen,
getFullscreenElement,
vendorify,
} from './fullscreen';
import { injectIntl, InjectedIntlProps } from 'react-intl';
import { messages } from '../messages';
export interface CustomMediaPlayerProps {
readonly type: 'audio' | 'video';
readonly src: string;
readonly isHDActive?: boolean;
readonly onHDToggleClick?: () => void;
readonly isHDAvailable?: boolean;
readonly showControls?: () => void;
readonly isAutoPlay: boolean;
readonly isShortcutEnabled?: boolean;
}
export interface CustomMediaPlayerState {
isFullScreenEnabled: boolean;
}
export type ToggleButtonAction = () => void;
export class CustomMediaPlayer extends Component<
CustomMediaPlayerProps & InjectedIntlProps,
CustomMediaPlayerState
> {
videoWrapperRef?: HTMLDivElement | null;
state: CustomMediaPlayerState = {
isFullScreenEnabled: false,
};
componentDidMount() {
document.addEventListener(
vendorify('fullscreenchange', false),
this.onFullScreenChange,
);
}
componentWillUnmount() {
document.removeEventListener(
vendorify('fullscreenchange', false),
this.onFullScreenChange,
);
}
onFullScreenChange = () => {
const { isFullScreenEnabled: currentFullScreenMode } = this.state;
const isFullScreenEnabled = getFullscreenElement() ? true : false;
if (currentFullScreenMode !== isFullScreenEnabled) {
this.setState({
isFullScreenEnabled,
});
}
};
onTimeChange = (navigate: NavigateFunction) => (value: number) => {
navigate(value);
};
onVolumeChange = (setVolume: SetVolumeFunction) => (value: number) =>
setVolume(value);
shortcutHandler = (toggleButtonAction: ToggleButtonAction) => () => {
const { showControls } = this.props;
toggleButtonAction();
if (showControls) {
showControls();
}
};
renderHDButton = () => {
const { type, isHDAvailable, isHDActive, onHDToggleClick } = this.props;
if (type === 'audio' || !isHDAvailable) {
return null;
}
const primaryColor = isHDActive ? colors.B200 : colors.DN400;
const secondaryColor = isHDActive ? colors.white : colors.DN60;
return (
}
/>
);
};
renderVolume = ({ isMuted, volume }: VideoState, actions: VideoActions) => {
return (
}
/>
);
};
onFullScreenClick = () => toggleFullscreen(this.videoWrapperRef);
saveVideoWrapperRef = (el: HTMLDivElement | null) =>
(this.videoWrapperRef = el);
renderFullScreenButton = () => {
const {
intl: { formatMessage },
type,
} = this.props;
if (type === 'audio') {
return null;
}
const { isFullScreenEnabled } = this.state;
const icon = isFullScreenEnabled ? (
) : (
);
return (
);
};
renderSpinner = () => (
);
render() {
const {
type,
src,
isAutoPlay,
isShortcutEnabled,
intl: { formatMessage },
} = this.props;
return (
{(video, videoState, actions) => {
const {
status,
currentTime,
buffered,
duration,
isLoading,
} = videoState;
const isPlaying = status === 'playing';
const toggleButtonIcon = isPlaying ? (
) : (
);
const toggleButtonAction = isPlaying
? actions.pause
: actions.play;
const button = (
);
const shortcuts = isShortcutEnabled && [
,
,
];
return (
{video}
{isLoading && this.renderSpinner()}
{shortcuts}
{button}
{this.renderVolume(videoState, actions)}
{formatDuration(currentTime)} /{' '}
{formatDuration(duration)}
{this.renderHDButton()}
{this.renderFullScreenButton()}
);
}}
);
}
}
export default injectIntl(CustomMediaPlayer);