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.
I think you need to add
assets
to youronPressed
function.So, your
onPressed
code should looks like: