Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async play (trackId, addHistory = true) {
if (this.list.length === 0) return
if (trackId) {
let track = this.list.find((i) => i.id === trackId)
if (!track) {
return new Error('歌曲未添加到列表')
}
let playerQueenTrack = true
try {
await TrackPlayer.getTrack(track.id)
} catch (e) {
console.log('查询错误', e)
playerQueenTrack = false
}
if (!playerQueenTrack) {
if (!track.artwork) {
track.artwork = 'http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg'
}
console.log('add Track', track)
await TrackPlayer.add(track)
}
if (addHistory) {
this.history.push(this.trackId)
this._getCurrentTrackId(trackid => {
TrackPlayer.getTrack(trackid)
.then(track => {
this.setState({
screenStates_screenPlayerStates_pageQueueStates_currentPlayingTrack: track,
});
this._updateIndexOfCurrentPlayingItemState();
})
.catch(e => console.log(e));
});
};
this._onTrackChanged = TrackPlayer.addEventListener('playback-track-changed', async data => {
if (globals.shouldUIRespondToEvents) {
if (data.nextTrack) {
const track = await TrackPlayer.getTrack(data.nextTrack);
this.setState({
screenStates_screenPlayerStates_pageQueueStates_currentPlayingTrack: track,
});
}
this.getTrackPlayerQueueToState();
this.updateCurrentPlayingTrackState();
}
});
})
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
}
})
}
}
useTrackPlayerEvents(["playback-track-changed"], async event => {
if (event.type === TrackPlayer.TrackPlayerEvents.PLAYBACK_TRACK_CHANGED) {
const track = await TrackPlayer.getTrack(event.nextTrack);
setTrackTitle(track.title);
setTrackArtist(track.artist);
setTrackArtwork(track.artwork);
}
});
const onTrackChange = TrackPlayer.addEventListener('playback-track-changed', async (data) => {
const track = await TrackPlayer.getTrack(data.nextTrack);
setMusic(prevState => ({
...prevState,
selected: track
}));
});