颤振的“音频”目录,音频文件不起作用

时间:2020-01-24 07:55:13

标签: android-studio flutter

我有一个简单的flutter应用程序,当点击狗的Circle头像时会发出吠叫声。如果我按照以下方式将音频文件保存在pubspec.yaml文件中,那么它将起作用

flutter:
  uses-material-design: true
  assets:
    - assets/
    - images/

化身用FlatButton控件包装,并且onPressed方法是这样的:

 onPressed: (){
                  audioCache.play('bark.mp3');
                },

但是,当我将音频文件保存在“音频”目录中时,不会发生相同的情况,

flutter:
  uses-material-design: true
  assets:
    - audios/
    - images/

和onPressed方法:

onPressed: (){
                  audioCache.play('audios/bark.mp3');
                },

当我使用第二种方法保留音频文件时,会出现以下错误:

Cannot find asset: "assets/audios/bark.mp3"

可能是什么问题?

这是main.dart中的以下代码

import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
// The main function is the starting point.
void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  AudioCache audioCache;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    audioCache = AudioCache();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.indigo[300],
        body: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              FlatButton(
                onPressed: (){
                  audioCache.play('bark.mp3');
                },
                child: CircleAvatar(
                  radius: 50,
                  backgroundImage: AssetImage("images/doggo.jpg"),
                ),
              ),
              Text(
                "Roger Pupperino",
                style: TextStyle(
                  fontSize: 25,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                  fontFamily: 'Pacifico',
                ),
              ),
              Text(
                "Goodest Boi Ever".toUpperCase(),
                style: TextStyle(
                  fontFamily: 'SourceSansPro',
                  color: Colors.indigo,
                  letterSpacing: 2.5,
                ),
              ),
              SizedBox(
                height: 20,
                width: 200,
                child: Divider(
                  color: Colors.indigo[300],
                ),
              ),
              Card(
                margin: EdgeInsets.symmetric(vertical: 10,horizontal: 25),
                color: Colors.white,
                child: ListTile(
                  leading: Icon(Icons.phone, color: Colors.indigo),
                  title: Text(
                    "+880 13033 84426",
                    style: TextStyle(
                      color: Colors.indigo,
                      fontFamily: 'Source Sans Pro',
                      fontSize: 20,
                    ),
                  ),
                ),
              ),
              Card(
                margin: EdgeInsets.symmetric(vertical: 10,horizontal: 25),
                color: Colors.white,
                child: ListTile(
                  leading: Icon(Icons.mail, color: Colors.indigo),
                  title:  Text(
                    "roger.goodboi@pup.com",
                    style: TextStyle(
                      color: Colors.indigo,
                      fontFamily: 'Source Sans Pro',
                      fontSize: 20,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

下面有目录树图像。请注意,audios和images文件夹位于根目录中。图像工作正常,但音频不工作。

Flutter Project Directory Tree

2 个答案:

答案 0 :(得分:1)

AudioPlayers被硬编码到“ assets \”文件夹,因此您不能将音频文件放入另一个根文件夹。

https://github.com/SSolheim2000/AudioPlayer_Basic/

答案 1 :(得分:0)

我认为您遗漏了一些东西,尝试使用 OnPressed 方法

below code