UNPKG

react-native-audio-player-hooks

Version:

Audio player hooks & Utility Classes to play audio files, using libraries react-native-video, rxjs, with playlist handling, player controls.

21 lines (20 loc) 813 B
import { useEffect, useRef, useState } from 'react'; import { PLAYER } from '../constants'; import PlayerManager from '../Utils/PlayerManager'; function usePlaylist({ keyName = PLAYER, onChangedPlaylist, } = {}) { const [playlist, setPlaylist] = useState(); const keyNameRef = useRef(keyName); const onChangedPlaylistRef = useRef(onChangedPlaylist); useEffect(() => { const PlayerController = PlayerManager.getPlayer(keyNameRef.current); const subscription = PlayerController.playList$.subscribe(_playlist => { setPlaylist(_playlist); onChangedPlaylistRef.current && onChangedPlaylistRef.current(_playlist); }); return () => { subscription.unsubscribe(); }; }, []); return playlist; } export default usePlaylist;