issue The method ‘play’ isn’t defined for the type ‘AudioCache’.
import ‘package:flutter/material.dart’;
import ‘package:audioplayers/src/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: TextButton(
onPressed: () {
final player = AudioCache();
player.play('note1.wave');
},
child: Text('click me'),
),
),
),
),
);
}
}
4
Answers
There seems to be the issue with your import. Do import thisπ
import ‘package:audioplayers/audioplayers.dart’;
If the issue still exists, then use an older version of it.
I think version 0.19.0 should work for you.
@Raj if You are doing LinkedIn course by London App Brewery and Angela Yu then an exact version that would work perfectly would be 0.10.0
It’s the one used by Angela and it worked perfectly for me π
Wouldn’t try it though if not for @Zain Basharat Ali advice.
Thanks for Your tip! π
The code below is no longer valid from audioplayers v1.0.1
Instead U can do this
consider look in migration guide from audioplayers
AudioCache
is dead because of confusion in name. Now, if you want to play anaudio file
fromassets
you can use this.Use this instead of
AssetSource
if you want don’t want to play from assets.UrlSource
: get the audio from a remote URL from the InternetDeviceFileSource
: access a file in the user’s device, probably selected by a file pickerAssetSource
: play an asset bundled with your app, normally within the assets directoryBytesSource
(only some platforms): pass in the bytes of your audio directly (read it from anywhere).You can see more from
audioplayers
documentation