audioplayer icon indicating copy to clipboard operation
audioplayer copied to clipboard

Unable to load asset : assets/audio/note1.wav

Open wincynin opened this issue 4 years ago • 2 comments

I am trying to play local files in my xylophone app but it keeps throwing this error and doesn't play any note.

Error:

E/flutter (22928): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Unable to load asset: assets/audio/note1.wav
E/flutter (22928): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): #1      AudioCache.fetchToMemory (package:audioplayers/src/audio_cache.dart:89:22)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): #2      AudioCache.load (package:audioplayers/src/audio_cache.dart:115:31)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): #3      AudioCache.play (package:audioplayers/src/audio_cache.dart:159:17)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): 

main.dart:

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';

void main() {
  runApp(Xylophone());
}

class Xylophone extends StatefulWidget {
  const Xylophone({Key? key}) : super(key: key);

  @override
  _XylophoneState createState() => _XylophoneState();
}

void playSound(int noteNumber) {
  final player = AudioCache();
  player.play('audio/note$noteNumber.wav');
}

class _XylophoneState extends State<Xylophone> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Column(
            children: [
              Container(
                color: Colors.red,
                child: MaterialButton(
                  onPressed: () {
                    playSound(1);
                  },
                  child: Text('Do'),
                ),
              ),
              Container(
                color: Colors.orange,
                child: MaterialButton(
                  onPressed: () {
                    playSound(2);
                  },
                  child: Text('Re'),
                ),
              ),
              Container(
                color: Colors.yellow,
                child: MaterialButton(
                  onPressed: () {
                    playSound(3);
                  },
                  child: Text('Mi'),
                ),
              ),
              Container(
                color: Colors.green,
                child: MaterialButton(
                  onPressed: () {
                    playSound(4);
                  },
                  child: Text('Fa'),
                ),
              ),
              Container(
                color: Colors.teal,
                child: MaterialButton(
                  onPressed: () {
                    playSound(5);
                  },
                  child: Text('Si'),
                ),
              ),
              Container(
                color: Colors.blue,
                child: MaterialButton(
                  onPressed: () {
                    playSound(6);
                  },
                  child: Text('La'),
                ),
              ),
              Container(
                color: Colors.purple,
                child: MaterialButton(
                  onPressed: () {
                    playSound(7);
                  },
                  child: Text('Sol'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

And pubsec.yaml:

name: xylophone
description: A new Flutter project.

publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  audioplayers: ^0.19.1

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

assets:
  - assets/audio

Please provide detailed answers and helpful solutions.

wincynin avatar Aug 29 '21 18:08 wincynin

If this is still relevant, a folder entry under the assets block must end with a forward slash. For example:

# correct
assets:
  - assets/audio/ 

And not

# incorrect
assets:
  - assets/audio

More importantly, note that this question is posted under the audioplayer package, when your code appears to be using a different package called audioplayers

boaz-amit avatar Sep 26 '21 11:09 boaz-amit

In Android: if file name is note1.wav,you can try 1:change [ player.play('audio/note1.wav')] to [player.play('flutter_assets/audio/note1.wav')]. 2:change ··mediaPlayer.setDataSource(url);··to ·· AssetFileDescriptor fd = activity.getAssets().openFd(url); mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());·· i have no idea in iOS.

watermelonQAQ avatar Dec 27 '21 02:12 watermelonQAQ