-1

why am i havng this error Exception has occurred. FlutterError (Unable to load asset: "assets/assets/audio/ontheroad.mp3". The asset does not exist or has empty data.)

import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:musica/models/song.dart';

class PlaylistProvider extends ChangeNotifier {
  //playlist of songs

  final List<Song> _playlist = [
    Song(
      songName: "Hollywoods Bleeding",
      artistName: "Post Malone",
      albumArtImagePath: "assets/image/posty.jpg",
      audioPath: "assets/audio/bleeding.mp3",
    ),
    Song(
      songName: "Ghetto christmas",
      artistName: "Xxxtentacion",
      albumArtImagePath: "assets/image/x2.jpg",
      audioPath: "assets/audio/ghetto.mp3",
    ),
    Song(
      songName: "On the road",
      artistName: "Post Malone",
      albumArtImagePath: "assets/image/posty3.jpg",
      audioPath: "assets/audio/ontheroad.mp3",
    ),
    Song(
      songName: "Sad!",
      artistName: "Xxxtentacion",
      albumArtImagePath: "assets/image/x.jpg",
      audioPath: "assets/audio/sad.mp3",
    ),
  ];

  int? _currentSongIndex;

  //audio player
  final AudioPlayer _audioPlayer = AudioPlayer();
  //duration
  Duration _currentDuration = Duration.zero;
  Duration _totalDuration = Duration.zero;
  //constructor
  PlaylistProvider() {
    listenToDuration();
  }

  //initially not playing
  bool _isPlaying = false;

  //play the song
  void play() async {
     final String path = _playlist[_currentSongIndex!].audioPath;
     await _audioPlayer.stop(); //stop current song
     await _audioPlayer.play(AssetSource(path)); //why am i getting errors ?
    _isPlaying = true;
    notifyListeners();
  }

so i am having issue on the await await _audioPlayer.play(AssetSource(path)); //why am i getting errors ? can anyone help me out its not leading the song in my music player app

assets:
    - assets/audio/
    - assets/image/

_playlist: This private variable holds a list of Song objects representing the playlist. _currentSongIndex: Another private variable to store the index of the currently playing song in the playlist. _audioPlayer: An instance of the AudioPlayer class used for playing audio files. _currentDuration: Tracks the current playback position of the song. _totalDuration: Stores the total duration of the currently playing song. PlaylistProvider(): The constructor likely initializes some values or sets up listeners. _isPlaying: A boolean flag indicating whether the music is currently playing. play(): This function takes care of playing a song. It retrieves the path from the current song in the playlist, stops any currently playing song, starts playing the new song using AssetSource, updates the playing state, and notifies listeners about changes.

4
  • Kindly check again to ensure that the assets folder is located within the assets directory. Commented Aug 13, 2024 at 11:00
  • Can you share song.dart? You seem to prefix 'asset/' twice. If you can also share a screen of the project folders and the pubspec.yaml. Commented Aug 13, 2024 at 15:51
  • here is my song.dart code ** ` class Song { final String songName; final String artistName; final String albumArtImagePath; final String audioPath; Song({ required this.songName, required this.artistName, required this.albumArtImagePath, required this.audioPath, }); } `** Commented Aug 13, 2024 at 16:12
  • DroidFlutte sir it works thank you very much Commented Aug 13, 2024 at 16:18

1 Answer 1

2

"AssetSource(...)" has pre-defined assets/ in library.

So, Use only assets/audio/ontheroad.mp3 this path.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.