Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return async (dispatch, getState) => {
try {
dispatch(playbackState(await TrackPlayer.getState()));
dispatch(playbackTrack(await TrackPlayer.getCurrentTrack()));
} catch(e) {
// The player is probably not yet initialized
// which means we don't have to update anything
console.log(e);
}
};
}
import TrackPlayer from 'react-native-track-player'
import { AsyncStorage } from 'react-native'
import { create } from 'mobx-persist'
import music from './music'
TrackPlayer.setupPlayer({})
.then(() => {
TrackPlayer.updateOptions({
capabilities: [
TrackPlayer.CAPABILITY_PLAY,
TrackPlayer.CAPABILITY_PAUSE,
TrackPlayer.CAPABILITY_SKIP_TO_NEXT,
TrackPlayer.CAPABILITY_SKIP_TO_PREVIOUS
]
})
})
const hydrate = create({ storage: AsyncStorage })
hydrate('music', music)
.then(() => {
// console.log(music.list.slice())
//use this to save listened tracks into files
console.log('data.type: ' + data.type);
if (data.type == 'playback-error' && data.code == 'playback-source') {
globals.isFetchingURL = true;
_fetchURLs(true, 0, 0, () => TrackPlayer.play(), () => (globals.isFetchingURL = false));
}
if (data.type == 'playback-state') {
// Update the UI with the new state
console.log(JSON.stringify(data));
if (data.state == TrackPlayer.STATE_NONE) {
console.log('STATE_NONE');
}
if (data.state == TrackPlayer.STATE_PLAYING) {
console.log('STATE_PLAYING');
TrackPlayer.getCurrentTrack().then(currentTrackId => {
TrackPlayer.getTrack(currentTrackId).then(track => {
if (
previousPlayingTrack.title !== track.title ||
previousPlayingTrack.artist !== track.artist ||
previousPlayingTrack.artwork !== track.artwork
) {
this.writeRecentTrack(
new Date().getTime(),
track.title,
track.artist,
track.artwork,
track.videoId
);
}
componentDidMount() {
this.checkPreviousAndNext();
this.onPlayerUpdate = TrackPlayer.addEventListener('playback-state', (data) => this.onPlaybackStatusUpdate(data));
this.onPlayerError = TrackPlayer.addEventListener('playback-error', (data) => this.onPlaybackError(data));
this.onPlayerQueueEnded = TrackPlayer.addEventListener('playback-queue-ended', (data) => this.onQueueEnded(data));
TrackPlayer.addEventListener('remote-play', () => this.resumeTrack());
TrackPlayer.addEventListener('remote-pause', () => this.pauseTrack());
TrackPlayer.addEventListener('remote-previous', () => this.playPreviousTrack());
TrackPlayer.addEventListener('remote-next', () => this.playNextTrack());
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
// TrackPlayer.addEventListener('remote-stop', () => TrackPlayer.destroy());
}
const time = await TrackPlayer.getPosition()
if (time <= 3) {
TrackPlayer.skipToPrevious()
} else {
TrackPlayer.seekTo(0)
}
})
TrackPlayer.addEventListener('remote-seek', ({ position }) =>
TrackPlayer.seekTo(position)
)
if (Platform.OS !== 'ios') {
// this event type is not supported on iOS
TrackPlayer.addEventListener('remote-duck', ({ ducking }) => {
TrackPlayer.setVolume(ducking ? 0.5 : 1)
})
}
TrackPlayer.addEventListener('playback-state', () =>
dispatch(playbackState())
)
TrackPlayer.addEventListener('playback-track-changed', () => {
dispatch(playbackTrack())
})
TrackPlayer.addEventListener('playback-queue-ended', ({ position }) => {
dispatch(playbackQueueEnded(position))
})
TrackPlayer.addEventListener('playback-error', error => {
TrackPlayer.play();
});
TrackPlayer.addEventListener("remote-pause", () => {
TrackPlayer.pause();
});
TrackPlayer.addEventListener("remote-stop", () => {
TrackPlayer.stop();
});
TrackPlayer.addEventListener("remote-seek", () => {
TrackPlayer.seekTo(data.position);
});
TrackPlayer.addEventListener("remote-jump-backward", () => {
TrackPlayer.seekTo(data.position - 15);
});
TrackPlayer.addEventListener("remote-jump-forward", () => {
TrackPlayer.seekTo(data.position + 15);
});
if (Platform.OS !== "ios") {
// this event type is not supported on iOS
TrackPlayer.addEventListener("remote-duck", () => {
TrackPlayer.setVolume(data.ducking ? 0.5 : 1);
});
}
TrackPlayer.addEventListener("playback-state", async data => {
if (data.state) {
componentDidMount() {
this.checkPreviousAndNext();
this.onPlayerUpdate = TrackPlayer.addEventListener('playback-state', (data) => this.onPlaybackStatusUpdate(data));
this.onPlayerError = TrackPlayer.addEventListener('playback-error', (data) => this.onPlaybackError(data));
this.onPlayerQueueEnded = TrackPlayer.addEventListener('playback-queue-ended', (data) => this.onQueueEnded(data));
TrackPlayer.addEventListener('remote-play', () => this.resumeTrack());
TrackPlayer.addEventListener('remote-pause', () => this.pauseTrack());
TrackPlayer.addEventListener('remote-previous', () => this.playPreviousTrack());
TrackPlayer.addEventListener('remote-next', () => this.playNextTrack());
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
// TrackPlayer.addEventListener('remote-stop', () => TrackPlayer.destroy());
}
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);
}
}
onPanResponderRelease: () => {
// end
TrackPlayer.seekTo((duration / 100) * percent)
setMoveSlider(false)
},
onPanResponderGrant: ({ nativeEvent: { locationX, locationY } }) =>
TrackPlayer.play()
break
case 'remote-pause':
TrackPlayer.pause()
break
case 'remote-stop':
TrackPlayer.stop()
break
case 'remote-next':
TrackPlayer.skipToNext()
break
case 'remote-previous':
TrackPlayer.skipToPrevious()
break
case 'remote-seek':
TrackPlayer.seekTo(data.position)
break
case 'remote-duck':
TrackPlayer.setVolume(data.ducking ? 0.5 : 1)
break
case 'playback-state':
dispatch(playbackState(data.state))
break
case 'playback-track-changed':
dispatch(playbackTrack(data.nextTrack))
break
case 'playback-queue-ended':
// finish
break
case 'playback-error':