0

首先嗨,

启动服务

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 ?

这是类似的东西,但我无法让它工作

在 just_audio 和 audio_service 中播放之前,如何每次从 API 获取歌曲详细信息

4

1 回答 1

0

这就是我解决问题的方法。

 @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]);

  }
于 2021-08-28T15:51:40.767 回答