raiyan106
raiyan106

Reputation: 133

Flutter "audio" directory for audio files not working

I have a simple flutter app that plays a bark noise when tapped on the Circle avatar of the dog. If I keep my audio files in pubspec.yaml file in the following way then it works

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

The avatar is wrapped with a FlatButton Widget and the onPressed method is like this:

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

BUT, the same does not happen when I keep the audio file in the "audio" directory, like this:

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

And the onPressed method:

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

When I keep the audio file using the second method, it gives me the following error:

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

What can be the problem?

Here is the following code in 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(
                    "[email protected]",
                    style: TextStyle(
                      color: Colors.indigo,
                      fontFamily: 'Source Sans Pro',
                      fontSize: 20,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Below I have the directory tree image. Notice that the audios and images folder are in the root directory. Images work fine but Audios are not working.

Flutter Project Directory Tree

Upvotes: 3

Views: 1649

Answers (2)

SSolheim
SSolheim

Reputation: 85

AudioPlayers is hardcoded to the "assets\" folder, so you can't put your audio files into a different root folder.

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

Upvotes: 1

Aamil Silawat
Aamil Silawat

Reputation: 8229

I think you missing something try below code on OnPressed method

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

Upvotes: 0

Related Questions