Flutter : audioservice. How to play local files?

412

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

Share:
412
killian
Author by

killian

Updated on December 28, 2022

Comments

  • killian
    killian over 1 year

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

    }

  • killian
    killian about 3 years
    Hello, thank you for your answer. I don't think this is the right plugin because I want to play my songs in background like a normal audio player.