no audio sound after importing audioplayers package in flutter

3.3k Views Asked by At

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.

3

There are 3 best solutions below

3
On

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');
              },
0
On

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

0
On

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.