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

时间:2021-03-12 10:04:30

标签: flutter audio local

我想播放本地文件的播放列表。 在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>

我知道这个错误来自 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);
}

我尝试像这样设置音频源,但仍然出现错误:

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.

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

}

1 个答案:

答案 0 :(得分:0)

我正在使用我的游戏,我更喜欢这种方式,

import 'package:assets_audio_player/assets_audio_player.dart';

class SoundManager {
  static playSilencer() async {
    final fileSilencer = "assets/sound/Gun_Silencer.mp3";

    AssetsAudioPlayer player = AssetsAudioPlayer();
    player.open(Audio(fileSilencer));
  }

  static playLuger() async {
    final fileLuger = "assets/sound/Gun_Luger.mp3";
    AssetsAudioPlayer player = AssetsAudioPlayer();
    player.open(Audio(fileLuger));
  }
}

就这样通过你想玩的地方:

SoundManager.playLuger();

这是我的代码: