Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

javascript - How to skip to the next song with shuffle on? REACT-NATIVE

im building a music player, and im having a problem with this, cant think a way to solve this, i have the shuffle mode, when the shuffle is on and I press next song, it goes to the next song, and when the track is over, it validates if shuffle is on, if its, repeat the track, if not, next song, the problem here is when i select another track from the queue, because when i do that, the listener will execute, validating again if shuffle is on and next is off, causing a loop, i can only go to the next song pressing next and not from the queue, heres the code:

state = {
  shuffle: true,
  next_song: false,
  repeat_song: "",
}

if (this.state.repeat && !this.state.next_song) { //if user want to repeat a track
                await TrackPlayer.pause();
                await TrackPlayer.skip(this.state.repeat_song);
                TrackPlayer.play();
            }else {
                let {title} = await TrackPlayer.getTrack(current_song);
                let {current_track_id, current_track, cover, artist} = await this.get_info_track(tracks, title);
                await AsyncStorage.setItem("last_song", JSON.stringify(current_track));
                await AsyncStorage.setItem("last_song_duration", JSON.stringify(0));
                this.setState({
                    current_track_id: current_track_id, 
                    current_track: current_track, 
                    cover: cover, 
                    artist: artist,
                    repeat_song_id: current_song,
                    last_song_duration: 0,
                    next_song: false,
                });
            }

update_next_song = () => {    //this will execute when the user press next, once is executed will be passed to false again 
    this.setState({next_song: true});
}

then problem is when I select from the queue, next song is already set to false, and the first condition will always be true, so cant think a way to do this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hi you can put array of Songs in

 TrackPlayer.add(Array). 

and use

TrackPlayer.skipToNext()
TrackPlayer.skipToPrevious();

it will work according to your requirements thanks :-)

 skipToNext = async () => {
    try {
      await TrackPlayer.skipToNext();
      this.props.SetProgress('pasued')
    } catch (_) {
    }
  }

  skipToPrevious = async () => {
    try {
      await TrackPlayer.skipToPrevious();
      this.props.SetProgress('pasued')
    } catch (_) { }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...