2

I have imported a package from https://pub.dartlang.org/ called as audioplayers. I have added the dependecies in my .yaml file dependencies:

flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  audioplayers: ^0.15.1
assets:
    - assets/

note1.wav is my audio file in assets folder.

Then I run the command flutter pub get in my terminal.

Below is my flutter code:

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

void main() => runApp(XylophoneApp());

class XylophoneApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: FlatButton(
              onPressed: () {
                final player = AudioCache();
                player.play('note1.wav');
              },
              child: Text('click me'),
            ),
          ),
        ),
      ),
    );
  }
}

But there is no sound, instead I am getting the following error:

iOS => call startHeadlessService, playerId e24a78e3-1f9f-439a-8f55-c4a55386da4b
iOS => call play, playerId e24a78e3-1f9f-439a-8f55-c4a55386da4b
play!
isLocal: 1 1
volume: 1.000000 1
position: 0 (null)
setUrl /Users/pratteekshaurya/Library/Developer/CoreSimulator/Devices/13364636-6425-40FF-A7EC-9C2498EFA847/data/Containers/Data/Application/B2C9DBA6-8B70-4D3B-9463-5754D4556893/Library/Caches/note1.wav
player status: 1
iOS -> updateDuration...3.750000
iOS -> invokechannel
iOS -> onSoundComplete...
flutter: Fatal Error: Callback lookup failed!

I am new to flutter and I am not able to figure out what is the problem because I followed the exact given in the documents.

spydon
  • 9,372
  • 6
  • 33
  • 63
pratteek shaurya
  • 850
  • 2
  • 12
  • 34

3 Answers3

1

In my case the android simulator was the problem. to ensure that is case try rendering in a web app version and you will see the code works fine.

Yoosaf Abdulla
  • 3,722
  • 4
  • 31
  • 34
0

I think you need to add assets to your onPressed function.

So, your onPressed code should looks like:

              onPressed: () {
                final player = AudioCache();
                player.play('assets/note1.wav');
              },
hisam
  • 1,566
  • 1
  • 7
  • 21
  • I have alredy mentioned in my .yaml file : "assets: - assets/" , see I have written my .yaml file depencies in the question, so I don't have to write assets/note1.wav in my onPressed. – pratteek shaurya Jul 13 '20 at 13:59
  • 2
    That's not correct, the library goes by default to the assets folder and looks for the specified file, thus, adding an "asset" won't help – YonatanAr Jul 30 '20 at 22:57
  • @hisam I think you are wrong big time. Once added in the yaml file we don't have to mention. In fact you are adding a deliberate error by doing so. – Yoosaf Abdulla Feb 23 '22 at 09:40
0

First of all try to restart your app, not hot reload, but stop and run your app.

You shouldn`t add assets/ in your onPressed() property of the FlatButton (by the way the FlatButton class is deprecated, please use TextButton instead).

Also,if you are Android Studio user, please check in your 'External Libraries' folder inside 'Dart Packages' folder you should find 'audioplayers' folder as in screenshot below or if you are VisualStudio user here check 'Dependencies' folder. If you can`t find this package, it means that something wrong with installation, so try to reinstall this package

screenshot on how it should look like in Android Studio

screenshot on how it should look like in Visual Studio