Introduction
Flutter's audioplayers is a Flutter plugin that can play multiple simultaneous audio files, supporting Android, iOS, Linux, macOS, Windows and web platforms. It has the following characteristics:
- Audio can be loaded from local files, network resources or memory
- Can control volume, progress, speed and looping
- You can play multiple audios at the same time, or use a singleton pattern
- Can monitor playback status and position changes
- You can set the notification bar and lock screen control
Steps for usage
- To use audioplayers, you need to add dependency in pubspec.yaml:
dependencies:
audioplayers: ^4.0.1
Then import the package in your code:
import 'package:audioplayers/audioplayers.dart';
Next, you can create an AudioPlayer object and call the appropriate method to play the audio. For example, to play audio from a web resource, you can do this:
AudioPlayer audioPlayer = AudioPlayer();
audioPlayer.play('https://example.com/sound.mp3');
To play audio from a local file, you need to put the audio file under the assets folder first, and declare it in pubspec.yaml:
flutter:
assets:
- assets/sounds/
You can then use the AudioCache object to load and play local audio:
AudioCache audioCache = AudioCache();
audioCache.play('assets/sounds/sound.wav');
To control the playback of audio, you can use some methods of the AudioPlayer object, such as:
audioPlayer.pause(); // 暂停播放
audioPlayer.resume(); // 恢复播放
audioPlayer.stop(); // 停止播放
audioPlayer.seek(Duration(seconds: 10)); // 跳转到指定位置
audioPlayer.setVolume(0.5); // 设置音量
audioPlayer.setPlaybackRate(playbackRate: 1.5); // 设置速度
audioPlayer.setReleaseMode(ReleaseMode.LOOP); // 设置循环模式
To monitor audio status and position changes, you can use some properties of the AudioPlayer object, for example:
audioPlayer.onPlayerStateChanged.listen((state) {
// 处理不同的状态
});
audioPlayer.onDurationChanged.listen((duration) {
// 获取音频的总时长
});
audioPlayer.onAudioPositionChanged.listen((position) {
// 获取音频的当前位置
});
audioPlayer.onPlayerError.listen((msg) {
// 处理错误信息
});
To set notification bars and lock screen controls, you need to use the setNotification method of the AudioPlayer object and pass in a NotificationService object. NotificationService object contains some properties, such as title, author, cover image and so on. For example:
await audioPlayer.setNotification(
NotificationService(
title: 'Title',
author: 'Author',
imageUrl: 'https://example.com/image.jpg',
forwardSkipInterval: const Duration(seconds: 30),
backwardSkipInterval: const Duration(seconds: 30),
duration: duration,
enableNextTrackButton: true,
enablePreviousTrackButton: true,
),
);
Storage of resource files
- assets/audio directory - this is the directory assigned to audio resources, audioplayers will automatically search for audio files in this directory.
So if we have a click.mp3 audio, it can be placed in:
assets/audio/click.mp3
Then directly refer to the file name to play in the code:
audioCache.play('click.mp3');
2. Any directory, and then specify the full resource path - we can also place it in other directories, and then pass in the full resource path when playing:
audio/click.mp3
Play in the code:
audioCache.play('audio/click.mp3');
- On the network - we can also directly pass in a link to a network resource, and audioplayers will play the resource directly:
audioCache.play('https://example.com/click.mp3');
So in summary, in Flutter we can:
- Put audio resources in the assets/audio directory
- Put it in any directory, specify the full path
- Use web resources to play audio. Generally speaking, it is a better way to put resources under assets or within the same project. The use of network resources needs to consider issues such as network status and cache.