skip to Main Content

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

4

Answers


  1. Chosen as BEST ANSWER

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

  2. 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'));
    
    Login or Signup to reply.
  3. 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'));
    
    Login or Signup to reply.
  4. Just remove assets from your audio source like this:

    await player.setSource(AssetSource('music/music1.mp3'));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search