【问题标题】:Flutter AudioService get Fetch Url every timeFlutter AudioService 每次获取 Fetch Url
【发布时间】:2021-10-28 01:45:25
【问题描述】:

首先你好,

启动服务

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();

Override 这里添加了 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;
  }

相反,如何在不播放项目的情况下向 remote api 发出请求并更新 url

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

How to fetch song details every time from an API before playing in just_audio and audio_service

【问题讨论】:

  • 问题已解决。调试时,需要停止并再次运行。用于覆盖。我是如何解决这个问题的?

标签: flutter dart flutter-android 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]);

  }

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多