Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
TrackPlayer.registerEventHandler(async (data) => {
switch (data.type) {
case 'remote-play':
await music.play()
break
case 'remote-pause':
await music.pause()
break
case 'playback-state':
// WIP[] accurate case state
let durantion = await TrackPlayer.getDuration()
let position = await TrackPlayer.getPosition()
const state = await TrackPlayer.getState()
if(state === TrackPlayerType.STATE_PAUSED && durantion === position) {
await music.playNext()
}
break
}
await music.updateState()
})
_onPlayPause = async () => {
const state = this.props.playerState;
if (state === STATE_PLAYING) {
await TrackPlayer.pause();
} else if (state === STATE_PAUSED) {
const position = Math.round(await TrackPlayer.getPosition());
const duration = Math.round(await TrackPlayer.getDuration());
if (position === duration) {
// It's finished
this._playNewTrack();
} else {
await TrackPlayer.play();
}
} else {
this._playNewTrack();
}
};
async _updateProgress() {
try {
const data = {
position: await TrackPlayer.getPosition(),
bufferedPosition: await TrackPlayer.getBufferedPosition(),
duration: await TrackPlayer.getDuration(),
track: await TrackPlayer.getTrack(await TrackPlayer.getCurrentTrack()),
};
if (this._progressUpdates) {
this.setState(data);
}
} catch (e) {
// The player is probably not initialized yet, we'll just ignore it
}
}
async timeFunc() {
const {getProgress} = this.props
if(getProgress ) {
let state = await TrackPlayer.getState()
let position = await TrackPlayer.getPosition()
let duration = await TrackPlayer.getDuration();
getProgress(state,position,duration)
if(this.props.pageInfo){
this.setState({
value:(position/this.props.pageInfo.duration) * 100
})
}
}
}
const changePlaybackTrack = async () => {
const currentDuration = await TrackPlayer.getDuration()
setDuration(currentDuration)
}
return async dispatch => {
const trackId = await TrackPlayer.getCurrentTrack()
const duration = await TrackPlayer.getDuration()
const track = await TrackPlayer.getTrack(trackId)
dispatch({
type: types.TRACK,
payload: {
track,
duration
}
})
}
}