Use Local Audio in Flutter

Create Free Backend With Appwrite


In this section, you will learn to use local audio in your flutter application with the help of real-world example. You will create a simple audio player app. Here are the steps to create a app:

Step 1: Add Audio File To Your Project

You can download sample audio file from here or you can use your own audio file. Place the audio file inside assets/audio with the name sound.mp3.

Step 2: Install audioplayers Package

To use local audio in your app, the first step is to add the audioplayers package in your project. Open terminal at your project folder and execute the following command:

flutter pub add audioplayers
Step 3: Update Pubspec.yaml Assets

Now, you need to update the pubspec.yaml file. Go to your pubspec.yaml file and add the following code in it:

    - assets/audio/sound.mp3
Step 4: Import the audioplayers Package

Now, you need to import the audioplayers package in your flutter application. To import the audioplayers package, open your main.dart file and add the following import statement:

import 'package:audioplayers/audioplayers.dart';
Step 5: Code for Audio Player

Now, you need to create a audio player widget. Open main.dart file and add the following code in it:

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

void main() {

// Stateful widget to handle audio playback.
class MyApp extends StatefulWidget {
  _MyAppState createState() => _MyAppState(); // State creation.

class _MyAppState extends State<MyApp> {
  final AudioPlayer audioPlayer = AudioPlayer(); // Audio player instance.

  void dispose() {
    audioPlayer.dispose(); // Release audio player resources.

  // Plays an audio file.
  void playAudio() async {

  // Stops the audio playback.
  void stopAudio() async {
    await audioPlayer.stop();

  Widget build(BuildContext context) {
    // Builds the app's UI.
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Audio Player Example')),
        body: Center(
          child: Column(
            children: <Widget>[
                  onPressed: playAudio, child: const Text('Play Audio')),
                  onPressed: stopAudio, child: const Text('Stop Audio')),

Project Source Code

If you face any problem while creating this app, you can download the source code of this project from here.


Create a simple audio player app which can play and stop the audio file from the following URL:

You can simply create playAudioOnline() method and call it on button press. Here is the code for playAudioOnline() method:

void playAudioOnline() async {