1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

player seeking

This commit is contained in:
Fedor Katurov 2019-10-13 19:09:44 +07:00
parent 69ff9a7652
commit f445882a61
7 changed files with 48 additions and 12 deletions

View file

@ -4,7 +4,7 @@ import { selectPlayer } from '~/redux/player/selectors';
import * as PLAYER_ACTIONS from '~/redux/player/actions'; import * as PLAYER_ACTIONS from '~/redux/player/actions';
import { IFile } from '~/redux/types'; import { IFile } from '~/redux/types';
import { PLAYER_STATES } from '~/redux/player/constants'; import { PLAYER_STATES } from '~/redux/player/constants';
import { Player } from '~/utils/player'; import { Player, IPlayerProgress } from '~/utils/player';
import classNames from 'classnames'; import classNames from 'classnames';
import * as styles from './styles.scss'; import * as styles from './styles.scss';
import { Icon } from '~/components/input/Icon'; import { Icon } from '~/components/input/Icon';
@ -17,6 +17,7 @@ const mapDispatchToProps = {
playerSetFile: PLAYER_ACTIONS.playerSetFile, playerSetFile: PLAYER_ACTIONS.playerSetFile,
playerPlay: PLAYER_ACTIONS.playerPlay, playerPlay: PLAYER_ACTIONS.playerPlay,
playerPause: PLAYER_ACTIONS.playerPause, playerPause: PLAYER_ACTIONS.playerPause,
playerSeek: PLAYER_ACTIONS.playerSeek,
}; };
type Props = ReturnType<typeof mapStateToProps> & type Props = ReturnType<typeof mapStateToProps> &
@ -31,9 +32,10 @@ const AudioPlayerUnconnected = ({
playerSetFile, playerSetFile,
playerPlay, playerPlay,
playerPause, playerPause,
playerSeek,
}: Props) => { }: Props) => {
const [playing, setPlaying] = useState(false); const [playing, setPlaying] = useState(false);
const [progress, setProgress] = useState(0); const [progress, setProgress] = useState<IPlayerProgress>({ progress: 0, current: 0, total: 0 });
const onPlay = useCallback(() => { const onPlay = useCallback(() => {
if (current && current.id === file.id) { if (current && current.id === file.id) {
@ -45,13 +47,23 @@ const AudioPlayerUnconnected = ({
}, [file, current, status, playerPlay, playerPause, playerSetFile]); }, [file, current, status, playerPlay, playerPause, playerSetFile]);
const onProgress = useCallback( const onProgress = useCallback(
({ detail }) => { ({ detail }: { detail: IPlayerProgress }) => {
if (!detail || !detail.progress) return; if (!detail || !detail.total) return;
setProgress(detail.progress); setProgress(detail);
}, },
[setProgress] [setProgress]
); );
const onSeek = useCallback(
event => {
event.stopPropagation();
const { clientX, target } = event;
const { left, width } = target.getBoundingClientRect();
playerSeek((clientX - left) / width);
},
[playerSeek]
);
useEffect(() => { useEffect(() => {
const active = current && current.id === file.id; const active = current && current.id === file.id;
setPlaying(current && current.id === file.id); setPlaying(current && current.id === file.id);
@ -63,14 +75,16 @@ const AudioPlayerUnconnected = ({
}; };
}, [file, current, setPlaying, onProgress]); }, [file, current, setPlaying, onProgress]);
console.log({ progress });
return ( return (
<div onClick={onPlay} className={classNames(styles.wrap, { playing })}> <div onClick={onPlay} className={classNames(styles.wrap, { playing })}>
<div className={styles.playpause}> <div className={styles.playpause}>
{playing && status === PLAYER_STATES.PLAYING ? <Icon icon="pause" /> : <Icon icon="play" />} {playing && status === PLAYER_STATES.PLAYING ? <Icon icon="pause" /> : <Icon icon="play" />}
</div> </div>
<div className={styles.content}> <div className={styles.content}>
<div className={styles.progress}> <div className={styles.progress} onClick={onSeek}>
<div className={styles.bar} style={{ width: `${progress}%` }} /> <div className={styles.bar} style={{ width: `${progress.progress}%` }} />
</div> </div>
<div className={styles.title}>{file.url}</div> <div className={styles.title}>{file.url}</div>
</div> </div>

View file

@ -10,7 +10,7 @@
} }
.title { .title {
top: 15px; top: 20px;
opacity: 1; opacity: 1;
font-size: 12px; font-size: 12px;
padding-right: 140px; padding-right: 140px;
@ -46,7 +46,7 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
min-width: 0; min-width: 0;
padding: 0 $gap; padding: 0 $gap * 2 0 $gap;
position: relative; position: relative;
} }
@ -58,7 +58,7 @@
bottom: 0; bottom: 0;
left: 0; left: 0;
width: 100%; width: 100%;
opacity: 0.2; opacity: 0.7;
pointer-events: none; pointer-events: none;
touch-action: none; touch-action: none;
padding: 0 10px; padding: 0 10px;
@ -67,6 +67,7 @@
top: 0; top: 0;
text-align: left; text-align: left;
transition: all 0.5s; transition: all 0.5s;
font: $font_16_medium;
} }
.progress { .progress {
@ -77,6 +78,7 @@
touch-action: none; touch-action: none;
transition: opacity 0.5s; transition: opacity 0.5s;
left: 0; left: 0;
cursor: pointer;
&::after { &::after {
content: ' '; content: ' ';
@ -98,4 +100,5 @@
top: 5px; top: 5px;
border-radius: 5px; border-radius: 5px;
min-width: 10px; min-width: 10px;
transition: width 0.5s;
} }

View file

@ -5,10 +5,11 @@
padding: $gap; padding: $gap;
font-weight: 300; font-weight: 300;
font: $font_16_regular; font: $font_16_medium;
min-height: $comment_height; min-height: $comment_height;
box-sizing: border-box; box-sizing: border-box;
position: relative; position: relative;
color: #cccccc;
b { b {
font-weight: 600; font-weight: 600;

View file

@ -18,3 +18,8 @@ export const playerPlay = () => ({
export const playerPause = () => ({ export const playerPause = () => ({
type: PLAYER_ACTIONS.PAUSE, type: PLAYER_ACTIONS.PAUSE,
}); });
export const playerSeek = (seek: number) => ({
type: PLAYER_ACTIONS.SEEK,
seek,
});

View file

@ -6,6 +6,7 @@ export const PLAYER_ACTIONS = {
PLAY: `${prefix}PLAY`, PLAY: `${prefix}PLAY`,
PAUSE: `${prefix}PAUSE`, PAUSE: `${prefix}PAUSE`,
SEEK: `${prefix}SEEK`,
}; };
export const PLAYER_STATES = { export const PLAYER_STATES = {

View file

@ -1,6 +1,6 @@
import { takeLatest } from 'redux-saga/effects'; import { takeLatest } from 'redux-saga/effects';
import { PLAYER_ACTIONS } from './constants'; import { PLAYER_ACTIONS } from './constants';
import { playerSetFile } from './actions'; import { playerSetFile, playerSeek } from './actions';
import { Player } from '~/utils/player'; import { Player } from '~/utils/player';
import { getURL } from '~/utils/dom'; import { getURL } from '~/utils/dom';
@ -17,8 +17,14 @@ function pauseSaga() {
Player.pause(); Player.pause();
} }
function seekSaga({ seek }: ReturnType<typeof playerSeek>) {
Player.jumpToPercent(seek * 100);
console.log(seek * 100);
}
export default function* playerSaga() { export default function* playerSaga() {
yield takeLatest(PLAYER_ACTIONS.SET_FILE, setFileSaga); yield takeLatest(PLAYER_ACTIONS.SET_FILE, setFileSaga);
yield takeLatest(PLAYER_ACTIONS.PAUSE, pauseSaga); yield takeLatest(PLAYER_ACTIONS.PAUSE, pauseSaga);
yield takeLatest(PLAYER_ACTIONS.PLAY, playSaga); yield takeLatest(PLAYER_ACTIONS.PLAY, playSaga);
yield takeLatest(PLAYER_ACTIONS.SEEK, seekSaga);
} }

View file

@ -9,6 +9,12 @@ type PlayerEventListener = (
event: HTMLMediaElementEventMap[keyof HTMLMediaElementEventMap] event: HTMLMediaElementEventMap[keyof HTMLMediaElementEventMap]
) => void; ) => void;
export interface IPlayerProgress {
current: number;
total: number;
progress: number;
}
export class PlayerClass { export class PlayerClass {
public constructor() { public constructor() {
this.element.addEventListener('timeupdate', () => { this.element.addEventListener('timeupdate', () => {