killian
killian

Reputation: 55

Flutter : audioservice. How to play local files?

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

Answers (2)

Test inovark
Test inovark

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

  • For Uri.parse - it will need https or file... as it doesn't know what it is.
  • Setting filepath directly you will not need as it already knows it is a file.

Looks like android is able to differentiate but iOS probably not so add prefix file:// to your local path

Upvotes: 7

Md. Yeasin Sheikh
Md. Yeasin Sheikh

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

Related Questions