The method 'play' isn't defined for the type 'AudioCache'

15.6k views Asked by At

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

}

10

There are 10 answers

2
Zain Basharat Ali On

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.

0
Beata Krzyżosiak On

@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

audioplayers: 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! :-)

0
حويسن الطنطاوي On

The code below is no longer valid from audioplayers v1.0.1

final player = AudioCache();
player.play('note1.wave');

Instead U can do this

final player = AudioPlayer();
//
player.play(UrlSource('note1.wave'));

// If file located in assets folder like assets/sounds/note01.wave"
await player.play(AssetSource('sounds/note1.wave'));

consider look in migration guide from audioplayers

0
Hammad Zafar Bawara On

AudioCache is dead because of confusion in name. Now, if you want to play an audio file from assets you can use this.

// add this in imports
import 'package:audioplayers/audioplayers.dart';

// play audio
final player = AudioPlayer();
player.play(AssetSource('note1.wav'));

Use this instead of AssetSource if you want don't want to play from assets.

  1. UrlSource: get the audio from a remote URL from the Internet
  2. DeviceFileSource: access a file in the user's device, probably selected by a file picker
  3. AssetSource: play an asset bundled with your app, normally within the assets directory
  4. BytesSource (only some platforms): pass in the bytes of your audio directly (read it from anywhere).

You can see more from audioplayers documentation

0
AMG On

if you faces this problem with Tharwat Samy course , then here is the solution for the problem :

  • note that :

audioplayers 4.0.1

flutter 3.7.10

  • change this code :

     Number(
     image: 'assets/images/numbers/number_one.png',
     enName: 'one',
     jpName: 'Ichi',
     sound: 'assets/sounds/numbers/number_one_sound.mp3'),
    

to :

    Number(
    image: 'assets/images/numbers/number_one.png',
    enName: 'one',
    jpName: 'Ichi',
    // remove 'assets/' from audio path .
    sound: 'sounds/numbers/number_one_sound.mp3'), 
  • do this change with all audio files paths .
  1. also change player code from :

     AudioCache player = AudioCache(prefix:'assets/sounds/numbers/');
     player. Play(number.sound);
    

to :

    final player = AudioPlayer();
    player.play(AssetSource(number.sound));

or you can also add path directly by :

    final player = AudioPlayer();
    // don't forget to delete 'assets/' from the path 
    player.play(AssetSource('sounds/numbers/number_one_sound.mp3'));

from one of Tharwat samy students provided by : AMG , https://web.facebook.com/mohamed.amged.351

0
Lewis Kori On

The code below uses AudioCache instead of AudioPlayer();

final player = AudioCache();
player.play('note1.wave');

You should change to ...

final player = AudioPlayer();
player.play('note1.wave');
0
Andrea Monterrosa On

AudioCache Class is meant to manage cache storage where it saves the audio files temporarily. Check AudioCache Class from Source Code.

AudioPlayer Class already uses AudioCache Class and implements it in the method setSourceAsset for files saved in the assets folder, this method saves it to the cache.

Then, notice it won't play it automatically, you need to play it manually.

Since the play() method returns the method resume() Check AudioPlayer Class from Source Code, you can play it manually by executing the method resume() directly.


Example:

For the file 'assets/note1.wav'. assets/ is prefixed by default

final player = AudioPlayer();
await player.setSourceAsset('note1.wav'); 
await player.resume();`
0
Salton On

If You are following Flutter Boot Camp by Dr. Angela Yu on Udemy Here is The Solution that I've incorporated and get it Done Correctly

AudioPlayer player = AudioPlayer();
player.play(AssetSource('note2.wav'));
1
Prashant Maurya On
return MaterialApp(
  home: Scaffold(
    appBar: AppBar(
      title: Text('My Xylophone'),
      backgroundColor: Colors.teal,
    ),
    body: SafeArea(
      child: Center(
        child: TextButton(
          onPressed: () {
            final player = AudioPlayer(); //insted of AudioCache() use AudioPlayer()
            player.play(AssetSource('note1.wav'));
          },
          child: Text('Click Here'),
        ),
      ),
    ),
  ),
);
0
Saleh Mohammed On

You cannot use AudioCache(); with player.play('note1.wave'); This is because the method 'play' is not defined in the AudioCache class.

Use the following class:

final player = AudioPlayer();
player.play(AssetSource('note1.wave'));

But make sure that you have added the assets for the audio track to the pubspec.yaml file:

 assets:
   - assets/sounds/

Or if the first one doesn't work try this one

assets:
  - assets/sounds/note1.wave