颤振:音频服务。如何播放本地文件?

kil*_*ian 4 audio local flutter

我想播放本地文件的播放列表。在android上,没有问题,我可以在Mediaitem(id)中添加本地文件并且它可以正常播放。但在 iOS 上它不起作用。

我收到这个错误:

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: PlatformException((-1002) 
unsupported URL, null, null, null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:581:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
<asynchronous suspension>
#2      AudioService.updateQueue (package:audio_service/audio_service.dart:911:5)
<asynchronous suspension>
Run Code Online (Sandbox Code Playgroud)

我知道这个错误来自 AudioService.updatequeue 以及当我设置 AudioSource 时但我不知道如何解决它。

void startAudioService() async{
   List<MediaItem> playlist = [];

   playlist.add(MediaItem(mylocalFile, ...));

   await AudioService.start(
     androidStopForegroundOnPause: true,
     backgroundTaskEntrypoint: _audioPlayerTaskEntrypoint,
     androidNotificationChannelName: 'AudioPlayer',
     androidNotificationColor: 0xFF2196f3,
     androidNotificationIcon: 'mipmap/ic_launcher',
     androidEnableQueue: true,
   );
   await AudioService.updateQueue(playlist);
   await AudioService.skipToQueueItem(playlist[widget.startIndex].id);
}
Run Code Online (Sandbox Code Playgroud)

我尝试像这样设置audioSource,但仍然收到错误:

AudioSource.uri(Uri.file(item.id));

error : [VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: PlatformException(NoSuchMethodError: The getter 'sequence' was called on null.
Run Code Online (Sandbox Code Playgroud)

onUpadeQueue :

@override
Future<void> onUpdateQueue(List<MediaItem> queue) async{
AudioServiceBackground.setQueue(_queue = queue);
await _player.setAudioSource(ConcatenatingAudioSource(
  children: queue.map((item) => AudioSource.uri(Uri.parse(item.id))).toList(),
));
Run Code Online (Sandbox Code Playgroud)

}

小智 7

将前缀添加到本地路径file://

喜欢 file://+/var/mobile/Containers/Data/Application/1635ABBE-D6AD-4276-BD52-0E8A5A89DF71/Documents/842647e8b1cd40f889c01aaecb3f9d4b.mp3

  • 因为Uri.parse- 它需要httpsfile......因为它不知道它是什么。
  • 您不需要直接设置文件路径,因为它已经知道它是一个文件。

看起来 android 能够区分,但 iOS 可能无法区分,所以file://在本地路径中添加前缀