Home > database >  flutter Problem in vs code (Unable to Load asset)
flutter Problem in vs code (Unable to Load asset)

Time:11-19

I'm having a problem in flutter on vs code I imported the audioplayers here's my pubspec.yaml

enter image description here

here's my homepage where I call the audio players

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

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.brown,
        title: Text('anghami'),
      ),
      body: Container(
        color: Colors.brown[200],
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Card(
                child: ListTile(
                  title: Text(
                    "benab",
                    style: TextStyle(color: Colors.red),
                  ),
                  //tileColor: Colors.red,
                  leading: Icon(Icons.music_note),
                  iconColor: Colors.red,
                  onTap: () async {
                    final player = AudioPlayer();
                    await player
                        .setSource(AssetSource('assets/music/music1.mp3'));
                  },
                ),
              ),
              
            ],
          ),
        ),
      ),
    );
  }
}

whenever i try to play the music from the phone I get this error

enter image description here

P.S: the vs code has no problem in loading images or using other type of assets .

i've tried using Audiocache it doesn't work especially they deleted it in the last version ^1.1.1 ,

CodePudding user response:

It seems you trying to load an asset in /assets/assets/music/ folder. My guess is that you want to load an asset in /assets/music/ folder and it's a simple mistake.

To fix that:

// Replace the relative path of your asset below:
// assets/music/music1.mp3 -> music/music1.mp3
await player.setSource(AssetSource('music/music1.mp3'));

CodePudding user response:

Just remove assets from your audio source like this:

await player.setSource(AssetSource('music/music1.mp3'));

CodePudding user response:

Firstly, create a assets directory on the root of your project , then create music folder.

Try to play with

await player.setSource(AssetSource('music/music1.mp3'));
  • Related