Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

AudioCache has no play method after updating audioplayers

ErrorThe method 'play' isn't defined for the type 'AudioCache'.

Short answer

Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them. Its play methods were removed. Create an AudioPlayer and call play with an AssetSource instead. Asset paths are resolved through the cache's default prefix.

  • Applies to audioplayers 1.0 and newer, Flutter 3.x stable, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which audioplayers version the project resolved, then find the old calls.

    flutter pub deps | grep audioplayers
    grep -rn "AudioCache" lib/
  2. 2Match what you see, then jump to its fix

    1. 1The method 'play' isn't defined for the type 'AudioCache'Old code calls AudioCache.play, which was removed in 1.0Fix
    2. 2The sound lives in another package's assets folder and is not foundA package's assets need their own prefixFix

What does the error look like?

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

Cause 1 of 2

Old code calls AudioCache.play, which was removed in 1.0

1Check

The migration guide says AudioCache kept only its job of turning assets into local files, and its own play methods and looping versions are gone. AudioPlayer now plays any Source.

grep -rn "\\.play(" lib/

2Fix

Replace the AudioCache call with an AudioPlayer and an AssetSource.

final player = AudioPlayer();
await player.play(AssetSource('audio/my-audio.wav'));

3Verify

The analyzer error is gone and the sound plays.

flutter analyze

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

A package's assets need their own prefix

1Check

The shared cache uses a default prefix of /assets, which suits assets bundled in your own app.

grep -n "assets" pubspec.yaml

2Fix

Give that player a cache with the package prefix.

player.audioCache = AudioCache(prefix: 'packages/OTHER_PACKAGE/assets/');

3Verify

The packaged sound plays.

flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Read the migration guide and changelog before a major audioplayers upgrade.
  • Create one AudioPlayer per sound that may overlap, and dispose it when done.
  • Run flutter analyze after each dependency upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.