Reputation: 55
I want to play a playlist of local files. On android, there is no problems, I can add a local file in Mediaitem(id) and it plays correctly . But on iOS it is not working.
I get that error :
[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>
I know this error comes from AudioService.updatequeue and when I set the AudioSource but I don't know how to resolve it.
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);
}
I tried to set the audioSource like this but I still get an error :
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(),
));
}
Upvotes: 4
Views: 1550
Reputation: 71
Add prefix to your local path with file://
like file://
+ /var/mobile/Containers/Data/Application/1635ABBE-D6AD-4276-BD52-0E8A5A89DF71/Documents/842647e8b1cd40f889c01aaecb3f9d4b.mp3
Uri.parse
- it will need https
or file
... as it doesn't know what it is.Looks like android is able to differentiate but iOS probably not so add prefix file://
to your local path
Upvotes: 7
Reputation: 63594
Im using for my game, I prefer this way,
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));
}
}
just pass where you would like play like this:
SoundManager.playLuger();
here is my code:
Upvotes: 1