Home > database >  How to play audio file from app asset folder in flutter?
How to play audio file from app asset folder in flutter?

Time:06-04

I am new to flutter and i want to play mp3 audio file from assets folder and i have used many libraries from pub dev but none of them is giving me results.I am seeing no help anywhere else about how to simply play/stop audio file on button click.

import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers/audio_cache.dart'; // this line is giving error
import 'package:flutter/material.dart';

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

@override
State<SelectAzanClass> createState() => _SelectAzanClassState();
}

class _SelectAzanClassState extends State<SelectAzanClass> {
  AudioPlayer player = AudioPlayer(); 
  AudioCache cache = new AudioCache();
@override
Widget build(BuildContext context) {
 return InkWell(
                  onTap: () {
                    player.play('path/mp3file');
                  },child: FittedBox(
                    fit: BoxFit.scaleDown,
                    child: Text(
                      "Mecca Adhan",
                      style: TextStyle(
                          fontFamily: customFontFamily,
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                    ),
                  ),
                ),
}

Following is the pubspec.yaml file dependency version dependencies: audioplayers: ^0.20.1 //pubspec.yaml

CodePudding user response:

I have just made this beautiful app to learn the music systems in flutter you can check it. music app.

in this app I have used : [just_audio: ^0.9.21][2] [audio_session: ^0.1.6 1][2]

just-audio is the highest recommended package for the music-apps in flutter you can play audio from online or local storage in this way...

final player = AudioPlayer();
var duration = await player.setUrl('https://music.com/bar.mp3');
var duration = await player.setFilePath('/path/to/file.mp3');
var duration = await player.setAsset('path/to/asset.mp3'); //Local Storage Audio.

if want to play audio using AUDIO-PLAYER package then watch this video from youtube

CodePudding user response:

use this package audio_player I use it in my app for play audio it run somthly with no issue and you can use asset audio like this

 static final AudioPlayer _player = AudioPlayer(playerId: "id");
_player.play(url); /// if you use network url it play network url and if you add asset path it play asset audio 


  • Related