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

Time:07-20

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/

CodePudding user response:

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

assets:
    - assets/audios/note1.wav

CodePudding user response:

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 .

  • Related