Flutter AudioService каждый раз получает Fetch Url

прежде всего привет,

Начать службу

      await AudioService.start(
      backgroundTaskEntrypoint: _audioPlayerTaskEntrypoint,
      params: {
        'index': globalIndex,
        'offline': offline,
        'quality': preferredQuality
      },
      androidNotificationChannelName: 'BlackHole',
      androidNotificationColor: 0xFF181818,
      androidNotificationIcon: 'drawable/ic_stat_music_note',
      androidEnableQueue: true,
      androidStopForegroundOnPause: stopServiceOnPause,
    );


      await AudioService.updateQueue(globalQueue);

      await AudioService.play();

Переопределить Здесь добавляется URL-адрес. Но если я отправлю запрос в api для всех элементов, производительность будет плохой.

       @override
  Future<void> onUpdateQueue(List<MediaItem> _queue) async {

    await AudioServiceBackground.setQueue(_queue);

    await AudioServiceBackground.setMediaItem(_queue[index!]);
    concatenatingAudioSource = ConcatenatingAudioSource(
      children: _queue
          .map((item)
      {
        return AudioSource.uri(
         Uri.parse(item.extras!['url'].toString()),
          tag: item);
      }
      )

          .toList(),
    );
    await _player.setAudioSource(concatenatingAudioSource);
    await _player.seek(Duration.zero, index: index);
    queue = _queue;
  }

Вместо этого, как я могу сделать запрос к удаленному api и обновить URL-адрес без воспроизведения элемента?

Вот что-то похожее, но я не мог заставить его работать

Как каждый раз получать информацию о песне из API перед воспроизведением в just_audio и audio_service

1 ответ

Вот как я решил свою проблему.

       @override
  Future<void> onSkipToQueueItem(String mediaId) async {

    final newIndex = queue.indexWhere((item) => item.id == mediaId);

    if (newIndex == -1) return;

    queue[newIndex].extras!['url'] = await SaavnAPI().get_mp3(queue[newIndex].extras!['url'].toString());


    AudioServiceBackground.setMediaItem(queue[newIndex]);
    _player.setUrl(queue[newIndex].extras!['url'].toString());
    await AudioServiceBackground.setQueue(queue);

    index = newIndex;
    _player.seek(Duration.zero, index: newIndex);
    if (!offline) addRecentlyPlayed(queue[newIndex]);

  }
Другие вопросы по тегам