2

I am using flutter plugin audioplayers: ^0.7.8, the below code is working in Android but not working in IOS. I run the code in real ios device and click the button. It suppose the play the mp3 file, but no sound at all. Please help to solve this issue.

I already set up the info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Here with the print out from the console:

  • flutter: finished loading, uri=file:///var/mobile/Containers/Data/Application/E3A576E2-0F21-44CF-AF99-319D539767D0/Library/Caches/demo.mp3
  • Syncing files to device iPhone...
  • flutter: _platformCallHandler call audio.onCurrentPosition {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, value: 0}
  • flutter: _platformCallHandler call audio.onError {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, value: AVPlayerItemStatus.failed}

Here with my code:

class _MyHomePageState extends State<MyHomePage> {
  AudioPlayer audioPlugin = AudioPlayer();
  String mp3Uri;

  @override
  void initState() {
    AudioPlayer.logEnabled = true;
    _load();
  }

  Future<Null> _load() async {
    final ByteData data = await rootBundle.load('assets/demo.mp3');
    Directory tempDir = await getTemporaryDirectory();
    File tempFile = File('${tempDir.path}/demo.mp3');
    await tempFile.writeAsBytes(data.buffer.asUint8List(), flush: true);
    mp3Uri = tempFile.uri.toString();
    print('finished loading, uri=$mp3Uri');
  }

  void _playSound() {
    if (mp3Uri != null) {
      audioPlugin.play(mp3Uri, isLocal: true,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Audio Player Demo Home Page'),
      ),
      body: Center(),
      floatingActionButton: FloatingActionButton(
        onPressed: _playSound,
        tooltip: 'Play',
        child: const Icon(Icons.play_arrow),
      ),
    );
  }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
GPH
  • 1,111
  • 6
  • 29
  • 49

4 Answers4

12

If you want to use a local file, you have to use AudioCache.

Looking at the documentation, it says at the bottom:

AudioCache

In order to play Local Assets, you must use the AudioCache class.

Flutter does not provide an easy way to play audio on your assets, but this class helps a lot. It actually copies the asset to a temporary folder in the device, where it is then played as a Local File.

It works as a cache because it keep track of the copied files so that you can replay then without delay.

To get audio to play, this is what I figured out what we need to do:

import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';

AudioCache audioPlayer = AudioCache();

void main() {
  runApp(new MyApp());
}




class MyApp extends StatefulWidget {
  @override _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override initState(){
    super.initState();
    audioPlayer.play("Jingle_Bells.mp3");
  }
  @override Widget build(BuildContext context) {
    //This we do not care about
  }
}

Important:

It automatically places 'assets/' in front of your path. This means you if you wanted to load assets/Jingle_Bells.mp3, you would simply put audioPlayer.play("Jingle_Bells.mp3");. If you entered audioPlayer.play("assets/Jingle_Bells.mp3"); instead, AudioPlayers would actually load assets/assets/Jingle_Bells.mp3 instead.

``

iProgram
  • 6,057
  • 9
  • 39
  • 80
  • thanks, i used your code and hear the sound, but the screen crash with the below error: The following assertion was thrown building MyApp(state: _MyAppState#a5271): MediaQuery.of() called with a context that does not contain a MediaQuery. No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). – GPH Oct 29 '18 at 08:38
  • @GPH This is now a different question due to how your widgets are laid out. Your best option is to ask a new question with the whole of your display code so we can see what is happening. Feel free to post the link to the new question here for me to help you. Also remember to vote up and accept this answer. – iProgram Oct 29 '18 at 08:45
  • Thank you for your help! I do need your help in printing of flutter. Please kindly see this question https://stackoverflow.com/questions/52903682/flutter-create-chinese-character-pdf-file-and-print-file-in-printer – GPH Oct 29 '18 at 08:59
  • @GPH That does not look like where the MediaQuery.of() error is coming from. – iProgram Oct 29 '18 at 09:10
  • this is not about MediaQuery, will it be ok for you to help? – GPH Oct 29 '18 at 09:11
  • 1
    If the file is not in the local asset in iOS, can it be played? – Daryl Wong Feb 06 '20 at 22:07
0

For iOS you must add file:// in front of your local url. For Android this approach will also work but it's not required.

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
Akismi
  • 1
  • 1
0

For iOS you must add file:// in front of your local url. For Android this approach will also work but it's not required.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34360694) – Saurabh Prajapati May 15 '23 at 04:30
0

For those using the iOS simulator. Try clicking the silent button on and off.

Leo
  • 76
  • 1
  • 7