Flutter Audio Cache Error with player.play

Viewed 2798

Hi this code works wonderfully with Angela in my Flutter course haha but for some reason the player.play('notes1.wav') isnt working for me.. tips? I'm getting this error..

error: The method 'play' isn't defined for the type 'Type'. (undefined_method at [xylophone] lib/main.dart:17)

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

I'm working on the same course! You've got the opening/closing brackets missing on the AudioCache declaration. It should be:

  final player = AudioCache();

I'd also do this for playing the audio, as it will stop making a sound after you press a few buttons:

    player.play('note$note.wav',
      mode: PlayerMode.LOW_LATENCY, 
      stayAwake: false);

It's better than the original code, but not perfect. I think there are audio 32 channels that get used up quite quickly - the about change seems to release them a bit quicker. Good luck with the rest of the course!

Migration Guide

dependencies: audioplayers: ^0.x.x

to

dependencies: audioplayers: ^1.0.1

https://github.com/bluefireteam/audioplayers/blob/main/migration_guide.md

enter image description here

AudioCache is dead, long live Sources One of the main changes was my desire to "kill" the AudioCache API due to the vast confusion that it caused with users (despite our best efforts documenting everything).

We still have the AudioCache class but its APIs are exclusively dedicated to transforming asset files into local files, cache them, and provide the path. It however doesn't normally need be used by end users because the AudioPlayer itself is now capable of playing audio from any Source.

What is a Source? It's a sealed class that can be one of:

UrlSource: get the audio from a remote URL from the Internet DeviceFileSource: access a file in the user's device, probably selected by a file picker AssetSource: play an asset bundled with your app, normally within the assets directory BytesSource (only some platforms): pass in the bytes of your audio directly (read it from anywhere). If you use AssetSource, the AudioPlayer will use its instance of AudioCache (which defaults to the global cache if unchanged) automatically. This unifies all playing APIs under AudioPlayer and entirely removes the AudioCache detail for most users.

Yes same case for me as The method 'play' isn't defined for the type 'AudioCache'.

@Raz answer is also not working.

enter image description here

AudioCache is obsolete now, try this

import 'package:audioplayers/audioplayers.dart';

final player = AudioPlayer();

player.play(AssetSource('note1.wav'));

AudioCache is deprecated. It will not work now. Instead use this code:

child: TextButton(
  onPressed: () async {
    final player = AudioPlayer();
    await player.play(
      AssetSource('note1.wav'),
    );
  },
  child: Text("Play me"),
),
Related