Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_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 resumeTrack() {
// play again if track has finished playing
let position = await TrackPlayer.getPosition();
if (position > 0) {
await TrackPlayer.play();
} else {
this.props.playTrack(this.props.currentTrack, false);
}
}
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 onPressPrev = async () => {
const time = await TrackPlayer.getPosition()
if (time <= 3) {
TrackPlayer.skipToPrevious()
} else {
TrackPlayer.seekTo(0)
}
}
_addOrRemoveSeconds = async seconds => {
let position = await TrackPlayer.getPosition();
TrackPlayer.seekTo(position + seconds);
};
setInterval(async () => {
const [position, duration] = await Promise.all([
TrackPlayer.getPosition(),
TrackPlayer.getDuration()
])
const { replay, shuffle, track } = getState().Player
if (position && duration && position >= duration) {
if (replay) {
await TrackPlayer.seekTo(0)
} else if (shuffle) {
let queue = await TrackPlayer.getQueue()
queue = queue.filter(item => item.id !== track.id)
const count = queue.length - 1
const index = Math.floor(Math.random() * count)
const trackId = queue[index].id
interval = setInterval(async () => {
if (!moveSlider) {
const current = Math.floor(await TrackPlayer.getPosition())
if (duration && current) {
setTime(current)
setPercent((current * 100) / Math.floor(duration))
}
}
}, 100)
break
interval = setInterval(async () => {
const position = await TrackPlayer.getPosition()
dispatch(updateTime(position))
}, 500)
}