Raj
Raj

Reputation: 91

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

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

}

Upvotes: 9

Views: 18119

Answers (9)

Prashant Maurya
Prashant Maurya

Reputation: 11

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

Upvotes: 1

Salton
Salton

Reputation: 1

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

Upvotes: 0

Andrea Monterrosa
Andrea Monterrosa

Reputation: 1

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();`

Upvotes: 0

Lewis Kori
Lewis Kori

Reputation: 9

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

Upvotes: 0

AMG
AMG

Reputation: 31

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

Upvotes: 2

Hammad Zafar Bawara
Hammad Zafar Bawara

Reputation: 513

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

Upvotes: 5

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

Upvotes: 29

Beata Krzyżosiak
Beata Krzyżosiak

Reputation: 1

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

Upvotes: 0

Zain Basharat Ali
Zain Basharat Ali

Reputation: 157

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.

Upvotes: 0

Related Questions