title: In flutter how can we use audio_service to fetch dynamic data
body: <pre><code> await AudioService.start(
        backgroundTaskEntrypoint: _audioPlayerTaskEntrypoint,
        androidNotificationChannelName: 'Audio Player',
        androidNotificationColor: 0xFF2196f3,
        androidNotificationIcon: 'mipmap/ic_launcher',
        params: getParams(),
      );
  </code></pre>
  <p>This is my code snippet where i am calling AudioService.start but i am unable to start service.
  i am fetching the audio item from firebase and want to load those as a list view to audio_service..But i am unable to do that.My class where i have defined a audio service extending the background service.</p>
  <pre><code>
  import 'package:audio_service/audio_service.dart';
  import 'package:just_audio/just_audio.dart';
  
  MediaControl playControl = MediaControl(
    androidIcon: 'drawable/ic_action_play_arrow',
    label: 'Play',
    action: MediaAction.play,
  );
  MediaControl pauseControl = MediaControl(
    androidIcon: 'drawable/ic_action_pause',
    label: 'Pause',
    action: MediaAction.pause,
  );
  MediaControl skipToNextControl = MediaControl(
    androidIcon: 'drawable/ic_action_skip_next',
    label: 'Next',
    action: MediaAction.skipToNext,
  );
  MediaControl skipToPreviousControl = MediaControl(
    androidIcon: 'drawable/ic_action_skip_previous',
    label: 'Previous',
    action: MediaAction.skipToPrevious,
  );
  MediaControl stopControl = MediaControl(
    androidIcon: 'drawable/ic_action_stop',
    label: 'Stop',
    action: MediaAction.stop,
  );
  
  class AudioPlayerTask extends BackgroundAudioTask {
    //
    var _queue = &lt;MediaItem&gt;[];
  
    int _queueIndex = -1;
  
    AudioPlayer _audioPlayer = new AudioPlayer();
    AudioProcessingState _skipState;
    bool _playing;
  
    bool get hasNext =&gt; _queueIndex + 1 &lt; _queue.length;
  
    bool get hasPrevious =&gt; _queueIndex &gt; 0;
  
    MediaItem get mediaItem =&gt; _queue[_queueIndex];
  
    StreamSubscription&lt;AudioPlaybackState&gt; _playerStateSubscription;
    StreamSubscription&lt;AudioPlaybackEvent&gt; _eventSubscription;
  
    @override
    void onStart(Map&lt;String, dynamic&gt; params) {
      print(&quot;-------------------------------------started&quot;);
      _queue.clear();
      List mediaItems = params['data'];
      for (int i = 0; i &lt; mediaItems.length; i++) {
        MediaItem mediaItem = MediaItem.fromJson(mediaItems[i]);
        _queue.add(mediaItem);
      }
      _playerStateSubscription = _audioPlayer.playbackStateStream
          .where((state) =&gt; state == AudioPlaybackState.completed)
          .listen((state) {
        _handlePlaybackCompleted();
      });
      _eventSubscription = _audioPlayer.playbackEventStream.listen((event) {
        final bufferingState =
        event.buffering ? AudioProcessingState.buffering : null;
        switch (event.state) {
          case AudioPlaybackState.paused:
            _setState(
                processingState: bufferingState ?? AudioProcessingState.ready,
                position: event.position);
            break;
          case AudioPlaybackState.playing:
            _setState(
                processingState: bufferingState ?? AudioProcessingState.ready,
                position: event.position);
            break;
          case AudioPlaybackState.connecting:
            _setState(
                processingState: _skipState ?? AudioProcessingState.connecting,
                position: event.position);
            break;
          default:
        }
      });
      AudioServiceBackground.setQueue(_queue);
      onSkipToNext();
    }
  
    @override
    void onPlay() {
      if (_skipState == null) {
        _playing = true;
        _audioPlayer.play();
      }
    }
  
    @override
    void onPause() {
      _playing = false;
      _audioPlayer.pause();
    }
  
    @override
    void onSkipToNext() async {
      skip(1);
    }
  
    @override
    void onSkipToPrevious() {
      skip(-1);
    }
  
    void skip(int offset) async {
      int newPos = _queueIndex + offset;
      if (!(newPos &gt;= 0 &amp;&amp; newPos &lt; _queue.length)) {
        return;
      }
      if (null == _playing) {
        _playing = true;
      } else if (_playing) {
        await _audioPlayer.stop();
      }
      _queueIndex = newPos;
      _skipState = offset &gt; 0
          ? AudioProcessingState.skippingToNext
          : AudioProcessingState.skippingToPrevious;
      AudioServiceBackground.setMediaItem(mediaItem);
      await _audioPlayer.setUrl(mediaItem.id);
      print(mediaItem.id);
      _skipState = null;
      if (_playing) {
        onPlay();
      } else {
        _setState(processingState: AudioProcessingState.ready);
      }
    }
  
    @override
    Future&lt;void&gt; onStop() async {
      _playing = false;
      await _audioPlayer.stop();
      await _audioPlayer.dispose();
      _playerStateSubscription.cancel();
      _eventSubscription.cancel();
      return await super.onStop();
    }
  
    @override
    void onSeekTo(Duration position) {
      _audioPlayer.seek(position);
    }
  
    @override
    void onClick(MediaButton button) {
      playPause();
    }
  
    @override
    Future&lt;void&gt; onFastForward() async {
      await _seekRelative(fastForwardInterval);
    }
  
    @override
    Future&lt;void&gt; onRewind() async {
      await _seekRelative(rewindInterval);
    }
  
    Future&lt;void&gt; _seekRelative(Duration offset) async {
      var newPosition = _audioPlayer.playbackEvent.position + offset;
      if (newPosition &lt; Duration.zero) {
        newPosition = Duration.zero;
      }
      if (newPosition &gt; mediaItem.duration) {
        newPosition = mediaItem.duration;
      }
      await _audioPlayer.seek(_audioPlayer.playbackEvent.position + offset);
    }
  
    _handlePlaybackCompleted() {
      if (hasNext) {
        onSkipToNext();
      } else {
        onStop();
      }
    }
  
    void playPause() {
      if (AudioServiceBackground.state.playing)
        onPause();
      else
        onPlay();
    }
  
    Future&lt;void&gt; _setState({
      AudioProcessingState processingState,
      Duration position,
      Duration bufferedPosition,
    }) async {
      print('SetState $processingState');
      if (position == null) {
        position = _audioPlayer.playbackEvent.position;
      }
      await AudioServiceBackground.setState(
        controls: getControls(),
        systemActions: [MediaAction.seekTo],
        processingState:
        processingState ?? AudioServiceBackground.state.processingState,
        playing: _playing,
        position: position,
        bufferedPosition: bufferedPosition ?? position,
        speed: _audioPlayer.speed,
      );
    }
  
    List&lt;MediaControl&gt; getControls() {
      if (_playing) {
        return [
          skipToPreviousControl,
          pauseControl,
          stopControl,
          skipToNextControl
        ];
      } else {
        return [
          skipToPreviousControl,
          playControl,
          stopControl,
          skipToNextControl
        ];
      }
    } this is my class
  }
  
  class AudioState {
    final List&lt;MediaItem&gt; queue;
    final MediaItem mediaItem;
    final PlaybackState playbackState;
  
    AudioState(this.queue, this.mediaItem, this.playbackState);
  }
  
  
  
  
  </code></pre>