|

How to Play Audio in Flutter | Full Audio Player Code Example

How to Play Audio in Flutter | Full Audio Player Code Example

To play audio in Flutter, you can use the audioplayers plugin, which provides a simple API to play audio files.

Here is an example of how to play audio in Flutter using the audioplayers plugin:

  1. Add the audioplayers dependency to your pubspec.yaml file:
dependencies:
  audioplayers: ^0.19.1
  1. Run flutter pub get to install the dependency.
  2. Import the audioplayers package:
import 'package:audioplayers/audioplayers.dart';
  1. Create an instance of the AudioPlayer class:
AudioPlayer audioPlayer = AudioPlayer();
  1. Load an audio file:
String audioUrl = "https://example.com/audio.mp3";
int result = await audioPlayer.play(audioUrl);
  1. Play the audio file:
audioPlayer.play(audioUrl);
  1. Pause the audio file:
audioPlayer.pause();
  1. Stop the audio file:
audioPlayer.stop();

Here’s an example of a full audio player code:

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

class AudioPlayerWidget extends StatefulWidget {
  final String url;

  AudioPlayerWidget({required this.url});

  @override
  _AudioPlayerWidgetState createState() => _AudioPlayerWidgetState();
}

class _AudioPlayerWidgetState extends State<AudioPlayerWidget> {
  AudioPlayer audioPlayer = AudioPlayer();

  @override
  void initState() {
    super.initState();
    playAudio();
  }

  void playAudio() async {
    await audioPlayer.play(widget.url);
  }

  void pauseAudio() async {
    await audioPlayer.pause();
  }

  void stopAudio() async {
    await audioPlayer.stop();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ElevatedButton(
          onPressed: playAudio,
          child: Text("Play"),
        ),
        ElevatedButton(
          onPressed: pauseAudio,
          child: Text("Pause"),
        ),
        ElevatedButton(
          onPressed: stopAudio,
          child: Text("Stop"),
        ),
      ],
    );
  }
}

You can use the AudioPlayerWidget like this:

AudioPlayerWidget(url: "https://example.com/audio.mp3");

Once you have the basic audio player functionality set up in your Flutter app, you can customize it further to suit your needs. Here are some additional features you may want to add:

  1. Volume control – You can use the setVolume method of the AudioPlayer class to control the volume of the audio player.
audioPlayer.setVolume(0.5); // Set volume to 50%
  1. Seek functionality – You can use the seek method of the AudioPlayer class to seek to a specific position in the audio file.
Duration position = Duration(seconds: 30);
audioPlayer.seek(position); // Seek to 30 seconds
  1. Playback speed control – You can use the setPlaybackRate method of the AudioPlayer class to control the playback speed of the audio player.
audioPlayer.setPlaybackRate(1.5); // Increase playback speed by 50%
  1. Display playback progress – You can use a slider widget to display the playback progress of the audio file. You can use the getCurrentPosition and getDuration methods of the AudioPlayer class to get the current position and duration of the audio file, and then use them to update the value of the slider widget.
Duration currentPosition = await audioPlayer.getCurrentPosition();
Duration duration = await audioPlayer.getDuration();
double progress = currentPosition.inMilliseconds / duration.inMilliseconds;
  1. Error handling – You should handle any errors that may occur while playing audio. You can use the onPlayerError property of the AudioPlayer class to listen for errors.
audioPlayer.onPlayerError.listen((error) {
  // Handle error
});

By adding these features, you can create a more complete audio player for your Flutter app.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *