Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Audio Cache Error with player.play

Tags:

flutter

dart

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'),
            ),
          ),
        ),
      ),
    );
  }
}
like image 341
Kyle Knoblauch Avatar asked Jul 06 '26 02:07

Kyle Knoblauch


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

like image 62
Raz Avatar answered Jul 08 '26 14:07

Raz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!