skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. @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! πŸ™‚

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search