[ERROR: Unable to load asset: assets/audios/note1.wav in flutter

Viewed 123

I have this question. My code actually working but when I click my button there is no new sound. I actually change the name of directory folder but it didn't work.

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'dart:math';
void main() => runApp(XylophoneApp());

class XylophoneApp extends StatelessWidget {

  void playSound () {
  final player = AudioPlayer();
  int soundNumber =  Random().nextInt(5) + 1;
  player.setSource(AssetSource('sounds/note$soundNumber.wav'));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(

            child:TextButton
              (onPressed: () {
              playSound();
              },
             child:const Text('Click Me')),
          ),
        ),
      ),
    );
  }
}

Here is the result:

E/flutter ( 8119): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] 
Unhandled Exception: Unable to load asset: assets/audios/note1.wav
E/flutter ( 8119): #0      PlatformAssetBundle.load 
(package:flutter/src/services/asset_bundle.dart:237:7)
E/flutter ( 8119): <asynchronous suspension> 

yaml file :

  assets:
    - assets/sounds/
3 Answers

You have to add this file in assets section in pubspec.yaml file

assets:
    - assets/audios/note1.wav

try to move your sub audio folder out of your former asset folder that comes by default . and place it direct to your project new folder like lib folder in the first level

sometimes flutter does not see the external files .

and for better work done clean your engine .

As I'm working in the Xylophone project I did this to run my APP:

  1. You can add new project and then rewrite your code line by line and debug it probably it is because of some change that you have done.
  2. Change the package I recommend you to add the new package in new projects

PS: If you are in windows before everything quit the Android Studio from the Task Manager and shut down your computer then start everything from the beginning

If you find new solution let me know

Thank you

Related