flutterpackageandroid-music-playerflutter-audioplayers

Flutter audioplayers next and previous


I creating a music player in flutter using audioplayers as package but couldn't find way to play next and previous song in the list.


Solution

  • For example lets say you have a list of audio files

    List<String> allAudio = [
    'https://luan.xyz/files/audio/coins.wav', 
     'https://luan.xyz/files/audio/laser', 
     'https://luan.xyz/files/audio/ambient_c_motion.mp3', 
     'https://luan.xyz/files/audio/nasa_on_a_mission.mp3'
    ];
    

    Now create an index to know which element is the current index

    int index = 0;
    

    Noe to change the source of the audio you just have to call

        await player.setSource(allAudio[index]);
    

    And set state.. when the next button is clicked add one to the index and call the above-mentioned code to set source..

    Make sure you dont increment if the length of list -1 is achieved ..

    You can use this package

    https://pub.dev/packages/audioplayers