简体   繁体   中英

flutter Problem in vs code (Unable to Load asset)

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

在此处输入图像描述

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

在此处输入图像描述

PS: 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, [enter image description here][https://i.stack.imgur.com/u9kKR.png]

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'));

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'));

Just remove assets from your audio source like this:

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

Hello guys the solutions here are useful, so the main problem that I had was in the path so after correction it loaded the asset in a normal way, but instead of just loading the asset you want it to play obviously and that's guaranteed by using:

 **final player = AudioPlayer();**

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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