mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
better audio player
This commit is contained in:
parent
87b9b5f514
commit
c84dfdd2ab
7 changed files with 54 additions and 14 deletions
|
@ -8,12 +8,15 @@ import pick from 'ramda/es/pick';
|
||||||
import { selectPlayer } from '~/redux/player/selectors';
|
import { selectPlayer } from '~/redux/player/selectors';
|
||||||
import * as PLAYER_ACTIONS from '~/redux/player/actions';
|
import * as PLAYER_ACTIONS from '~/redux/player/actions';
|
||||||
import { IPlayerProgress, Player } from '~/utils/player';
|
import { IPlayerProgress, Player } from '~/utils/player';
|
||||||
|
import path from 'ramda/es/path';
|
||||||
|
import { IFile } from '~/redux/types';
|
||||||
|
|
||||||
const mapStateToProps = state => pick(['status', 'file'], selectPlayer(state));
|
const mapStateToProps = state => pick(['status', 'file'], selectPlayer(state));
|
||||||
const mapDispatchToProps = {
|
const mapDispatchToProps = {
|
||||||
playerPlay: PLAYER_ACTIONS.playerPlay,
|
playerPlay: PLAYER_ACTIONS.playerPlay,
|
||||||
playerPause: PLAYER_ACTIONS.playerPause,
|
playerPause: PLAYER_ACTIONS.playerPause,
|
||||||
playerSeek: PLAYER_ACTIONS.playerSeek,
|
playerSeek: PLAYER_ACTIONS.playerSeek,
|
||||||
|
playerStop: PLAYER_ACTIONS.playerStop,
|
||||||
};
|
};
|
||||||
|
|
||||||
type IProps = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
|
type IProps = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
|
||||||
|
@ -23,6 +26,7 @@ const PlayerBarUnconnected: FC<IProps> = ({
|
||||||
playerPlay,
|
playerPlay,
|
||||||
playerPause,
|
playerPause,
|
||||||
playerSeek,
|
playerSeek,
|
||||||
|
playerStop,
|
||||||
file,
|
file,
|
||||||
}) => {
|
}) => {
|
||||||
const [progress, setProgress] = useState<IPlayerProgress>({ progress: 0, current: 0, total: 0 });
|
const [progress, setProgress] = useState<IPlayerProgress>({ progress: 0, current: 0, total: 0 });
|
||||||
|
@ -60,10 +64,10 @@ const PlayerBarUnconnected: FC<IProps> = ({
|
||||||
|
|
||||||
if (status === PLAYER_STATES.UNSET) return null;
|
if (status === PLAYER_STATES.UNSET) return null;
|
||||||
|
|
||||||
|
const metadata: IFile['metadata'] = path(['metadata'], file);
|
||||||
const title =
|
const title =
|
||||||
file.metadata &&
|
metadata &&
|
||||||
(file.metadata.title ||
|
(metadata.title || [metadata.id3artist, metadata.id3title].filter(el => !!el).join(' - '));
|
||||||
[file.metadata.id3artist, file.metadata.id3title].filter(el => !!el).join(' - '));
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.place}>
|
<div className={styles.place}>
|
||||||
|
@ -81,7 +85,7 @@ const PlayerBarUnconnected: FC<IProps> = ({
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={styles.close}>
|
<div className={styles.close} onClick={playerStop}>
|
||||||
<Icon icon="close" />
|
<Icon icon="close" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -63,6 +63,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.info {
|
.info {
|
||||||
|
flex: 1;
|
||||||
display: flex;
|
display: flex;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
|
@ -14,7 +14,7 @@ const mapStateToProps = state => ({
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapDispatchToProps = {
|
const mapDispatchToProps = {
|
||||||
playerSetFile: PLAYER_ACTIONS.playerSetFile,
|
playerSetFileAndPlay: PLAYER_ACTIONS.playerSetFileAndPlay,
|
||||||
playerPlay: PLAYER_ACTIONS.playerPlay,
|
playerPlay: PLAYER_ACTIONS.playerPlay,
|
||||||
playerPause: PLAYER_ACTIONS.playerPause,
|
playerPause: PLAYER_ACTIONS.playerPause,
|
||||||
playerSeek: PLAYER_ACTIONS.playerSeek,
|
playerSeek: PLAYER_ACTIONS.playerSeek,
|
||||||
|
@ -29,7 +29,7 @@ const AudioPlayerUnconnected = ({
|
||||||
file,
|
file,
|
||||||
player: { file: current, status },
|
player: { file: current, status },
|
||||||
|
|
||||||
playerSetFile,
|
playerSetFileAndPlay,
|
||||||
playerPlay,
|
playerPlay,
|
||||||
playerPause,
|
playerPause,
|
||||||
playerSeek,
|
playerSeek,
|
||||||
|
@ -43,8 +43,8 @@ const AudioPlayerUnconnected = ({
|
||||||
return playerPlay();
|
return playerPlay();
|
||||||
}
|
}
|
||||||
|
|
||||||
playerSetFile(file);
|
playerSetFileAndPlay(file);
|
||||||
}, [file, current, status, playerPlay, playerPause, playerSetFile]);
|
}, [file, current, status, playerPlay, playerPause, playerSetFileAndPlay]);
|
||||||
|
|
||||||
const onProgress = useCallback(
|
const onProgress = useCallback(
|
||||||
({ detail }: { detail: IPlayerProgress }) => {
|
({ detail }: { detail: IPlayerProgress }) => {
|
||||||
|
|
|
@ -6,6 +6,11 @@ export const playerSetFile = (file: IPlayerState['file']) => ({
|
||||||
file,
|
file,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const playerSetFileAndPlay = (file: IPlayerState['file']) => ({
|
||||||
|
type: PLAYER_ACTIONS.SET_FILE_AND_PLAY,
|
||||||
|
file,
|
||||||
|
});
|
||||||
|
|
||||||
export const playerSetStatus = (status: IPlayerState['status']) => ({
|
export const playerSetStatus = (status: IPlayerState['status']) => ({
|
||||||
type: PLAYER_ACTIONS.SET_STATUS,
|
type: PLAYER_ACTIONS.SET_STATUS,
|
||||||
status,
|
status,
|
||||||
|
@ -19,6 +24,14 @@ export const playerPause = () => ({
|
||||||
type: PLAYER_ACTIONS.PAUSE,
|
type: PLAYER_ACTIONS.PAUSE,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const playerStop = () => ({
|
||||||
|
type: PLAYER_ACTIONS.STOP,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const playerStopped = () => ({
|
||||||
|
type: PLAYER_ACTIONS.STOPPED,
|
||||||
|
});
|
||||||
|
|
||||||
export const playerSeek = (seek: number) => ({
|
export const playerSeek = (seek: number) => ({
|
||||||
type: PLAYER_ACTIONS.SEEK,
|
type: PLAYER_ACTIONS.SEEK,
|
||||||
seek,
|
seek,
|
||||||
|
|
|
@ -2,11 +2,14 @@ const prefix = 'PLAYER.';
|
||||||
|
|
||||||
export const PLAYER_ACTIONS = {
|
export const PLAYER_ACTIONS = {
|
||||||
SET_FILE: `${prefix}SET_FILE`,
|
SET_FILE: `${prefix}SET_FILE`,
|
||||||
|
SET_FILE_AND_PLAY: `${prefix}SET_FILE_AND_PLAY`,
|
||||||
SET_STATUS: `${prefix}SET_STATUS`,
|
SET_STATUS: `${prefix}SET_STATUS`,
|
||||||
|
|
||||||
PLAY: `${prefix}PLAY`,
|
PLAY: `${prefix}PLAY`,
|
||||||
PAUSE: `${prefix}PAUSE`,
|
PAUSE: `${prefix}PAUSE`,
|
||||||
SEEK: `${prefix}SEEK`,
|
SEEK: `${prefix}SEEK`,
|
||||||
|
STOP: `${prefix}STOP`,
|
||||||
|
STOPPED: `${prefix}STOPPED`,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const PLAYER_STATES = {
|
export const PLAYER_STATES = {
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { takeLatest } from 'redux-saga/effects';
|
import { takeLatest, put } from 'redux-saga/effects';
|
||||||
import { PLAYER_ACTIONS } from './constants';
|
import { PLAYER_ACTIONS, PLAYER_STATES } from './constants';
|
||||||
import { playerSetFile, playerSeek } from './actions';
|
import { playerSetFile, playerSeek, playerSetStatus } from './actions';
|
||||||
import { Player } from '~/utils/player';
|
import { Player } from '~/utils/player';
|
||||||
import { getURL } from '~/utils/dom';
|
import { getURL } from '~/utils/dom';
|
||||||
|
|
||||||
function setFileSaga({ file }: ReturnType<typeof playerSetFile>) {
|
function* setFileAndPlaySaga({ file }: ReturnType<typeof playerSetFile>) {
|
||||||
|
yield put(playerSetFile(file));
|
||||||
Player.set(getURL(file));
|
Player.set(getURL(file));
|
||||||
Player.play();
|
Player.play();
|
||||||
}
|
}
|
||||||
|
@ -17,13 +18,24 @@ function pauseSaga() {
|
||||||
Player.pause();
|
Player.pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stopSaga() {
|
||||||
|
Player.stop();
|
||||||
|
}
|
||||||
|
|
||||||
function seekSaga({ seek }: ReturnType<typeof playerSeek>) {
|
function seekSaga({ seek }: ReturnType<typeof playerSeek>) {
|
||||||
Player.jumpToPercent(seek * 100);
|
Player.jumpToPercent(seek * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function* stoppedSaga() {
|
||||||
|
yield put(playerSetStatus(PLAYER_STATES.UNSET));
|
||||||
|
yield put(playerSetFile(null));
|
||||||
|
}
|
||||||
|
|
||||||
export default function* playerSaga() {
|
export default function* playerSaga() {
|
||||||
yield takeLatest(PLAYER_ACTIONS.SET_FILE, setFileSaga);
|
yield takeLatest(PLAYER_ACTIONS.SET_FILE_AND_PLAY, setFileAndPlaySaga);
|
||||||
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);
|
yield takeLatest(PLAYER_ACTIONS.SEEK, seekSaga);
|
||||||
|
yield takeLatest(PLAYER_ACTIONS.STOP, stopSaga);
|
||||||
|
yield takeLatest(PLAYER_ACTIONS.STOPPED, stoppedSaga);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { store } from '~/redux/store';
|
import { store } from '~/redux/store';
|
||||||
import { playerSetStatus } from '~/redux/player/actions';
|
import { playerSetStatus, playerStopped } from '~/redux/player/actions';
|
||||||
import { PLAYER_STATES } from '~/redux/player/constants';
|
import { PLAYER_STATES } from '~/redux/player/constants';
|
||||||
|
|
||||||
type PlayerEventType = keyof HTMLMediaElementEventMap;
|
type PlayerEventType = keyof HTMLMediaElementEventMap;
|
||||||
|
@ -64,6 +64,11 @@ export class PlayerClass {
|
||||||
this.element.pause();
|
this.element.pause();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public stop = () => {
|
||||||
|
this.element.src = '';
|
||||||
|
this.element.dispatchEvent(new CustomEvent('stop'));
|
||||||
|
};
|
||||||
|
|
||||||
public getDuration = () => {
|
public getDuration = () => {
|
||||||
return this.element.currentTime;
|
return this.element.currentTime;
|
||||||
};
|
};
|
||||||
|
@ -83,5 +88,7 @@ const Player = new PlayerClass();
|
||||||
|
|
||||||
Player.on('play', () => store.dispatch(playerSetStatus(PLAYER_STATES.PLAYING)));
|
Player.on('play', () => store.dispatch(playerSetStatus(PLAYER_STATES.PLAYING)));
|
||||||
Player.on('pause', () => store.dispatch(playerSetStatus(PLAYER_STATES.PAUSED)));
|
Player.on('pause', () => store.dispatch(playerSetStatus(PLAYER_STATES.PAUSED)));
|
||||||
|
Player.on('stop', () => store.dispatch(playerStopped()));
|
||||||
|
Player.on('error', () => store.dispatch(playerStopped()));
|
||||||
|
|
||||||
export { Player };
|
export { Player };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue