简体   繁体   English

[错误:无法加载资产:flutter 中的资产/音频/note1.wav

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

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: yaml 文件:

  assets:
    - assets/sounds/

You have to add this file in assets section in pubspec.yaml file您必须将此文件添加到 pubspec.yaml 文件的资产部分

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并将其直接放置到您的项目新文件夹中,例如第一级的 lib 文件夹

sometimes flutter does not see the external files .有时flutter看不到外部文件。

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更改 package 我建议您在新项目中添加新的 package

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 PS:如果您在 windows 之前从任务管理器退出 Android Studio 并关闭计算机然后从头开始一切

If you find new solution let me know如果您找到新的解决方案,请告诉我

Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM