Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Unable to load asset : assets/audio/note1.wav #155

Open
Wincynin opened this issue Aug 29, 2021 · 2 comments
Open

Unable to load asset : assets/audio/note1.wav #155

Wincynin opened this issue Aug 29, 2021 · 2 comments

Comments

@Wincynin
Copy link

Wincynin commented Aug 29, 2021

I am trying to play local files in my xylophone app but it keeps throwing this error and doesn't play any note.

Error:

E/flutter (22928): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Unable to load asset: assets/audio/note1.wav
E/flutter (22928): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): #1      AudioCache.fetchToMemory (package:audioplayers/src/audio_cache.dart:89:22)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): #2      AudioCache.load (package:audioplayers/src/audio_cache.dart:115:31)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): #3      AudioCache.play (package:audioplayers/src/audio_cache.dart:159:17)
E/flutter (22928): <asynchronous suspension>
E/flutter (22928): 

main.dart:

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';

void main() {
  runApp(Xylophone());
}

class Xylophone extends StatefulWidget {
  const Xylophone({Key? key}) : super(key: key);

  @override
  _XylophoneState createState() => _XylophoneState();
}

void playSound(int noteNumber) {
  final player = AudioCache();
  player.play('audio/note$noteNumber.wav');
}

class _XylophoneState extends State<Xylophone> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Column(
            children: [
              Container(
                color: Colors.red,
                child: MaterialButton(
                  onPressed: () {
                    playSound(1);
                  },
                  child: Text('Do'),
                ),
              ),
              Container(
                color: Colors.orange,
                child: MaterialButton(
                  onPressed: () {
                    playSound(2);
                  },
                  child: Text('Re'),
                ),
              ),
              Container(
                color: Colors.yellow,
                child: MaterialButton(
                  onPressed: () {
                    playSound(3);
                  },
                  child: Text('Mi'),
                ),
              ),
              Container(
                color: Colors.green,
                child: MaterialButton(
                  onPressed: () {
                    playSound(4);
                  },
                  child: Text('Fa'),
                ),
              ),
              Container(
                color: Colors.teal,
                child: MaterialButton(
                  onPressed: () {
                    playSound(5);
                  },
                  child: Text('Si'),
                ),
              ),
              Container(
                color: Colors.blue,
                child: MaterialButton(
                  onPressed: () {
                    playSound(6);
                  },
                  child: Text('La'),
                ),
              ),
              Container(
                color: Colors.purple,
                child: MaterialButton(
                  onPressed: () {
                    playSound(7);
                  },
                  child: Text('Sol'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

And pubsec.yaml:

name: xylophone
description: A new Flutter project.

publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  audioplayers: ^0.19.1

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

assets:
  - assets/audio

Please provide detailed answers and helpful solutions.

@boaz-amit
Copy link

boaz-amit commented Sep 26, 2021

If this is still relevant, a folder entry under the assets block must end with a forward slash. For example:

# correct
assets:
  - assets/audio/ 

And not

# incorrect
assets:
  - assets/audio

More importantly, note that this question is posted under the audioplayer package, when your code appears to be using a different package called audioplayers

@watermelonQAQ
Copy link

watermelonQAQ commented Dec 27, 2021

In Android: if file name is note1.wav,you can try
1:change [ player.play('audio/note1.wav')] to [player.play('flutter_assets/audio/note1.wav')].
2:change ··mediaPlayer.setDataSource(url);··to ·· AssetFileDescriptor fd = activity.getAssets().openFd(url);
mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());··
i have no idea in iOS.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants