audioplayers
audioplayers copied to clipboard
working minimal examples
Can you please provide some working exaples of playing local assets ?
I have upgraded from audioplayers: ^0.20.1 to 1.0.1. I have no idea how to play a file. Reverted back to ^0.20.1.
.play now takes a source. For example:
audioplayer.play(AssetSource('foo.mp3'))
More sources can be found here: https://github.com/bluefireteam/audioplayers/blob/main/packages/audioplayers/lib/src/source.dart
To whomever needs some help, I transitioned like this. It's a quick and hacky way, so please don't judge too harshly. The code is using the static instance in a bad way, but I only need to play a few small files (2-4 seconds), a few times, so I did not invest more time in it.
Before
Sounds class
static final _instance = AudioCache(prefix: "assets/sounds/");
static Future<AudioPlayer> play(String sound) async {
//sound is actually a file name
return await _instance.play(sound);
}
static Future<AudioPlayer> loop(String sound) async {
//sound is actually a file name
return await _instance.loop(sound);
}
static Future<void> playListSilently(List<String> list) async {
Debug.d("AudioUtils.playListSilently list: $list");
list.forEach((e) async => await _instance.play(e, volume: 0));
}
After
Sounds class
static AudioPlayer _instance;
static AudioPlayer _newInstance() {
_instance = AudioPlayer();
_instance.audioCache = AudioCache(prefix: "assets/sounds/");
return _instance;
}
static AudioPlayer play(String sound) {
var instance = _newInstance();
instance.play(AssetSource(sound));
return instance;
}
static AudioPlayer loop(String sound) {
var instance = _newInstance();
instance.setReleaseMode(ReleaseMode.loop);
instance.play(AssetSource(sound));
return instance;
}
static void playListSilently(List<String> list) async {
Debug.d("Sounds.playListSilently list: $list");
//var instance = _newInstance();
//how to set back volume?
//list.forEach((e) async => await instance.play(e, volume: 0));
}
To start and stop the loop
_audioPlayer = Sounds.loop(Sounds.aeolCounter);
...
if (_audioPlayer != null) _audioPlayer.stop();
The method playListSilently
is there because it was a hack to load the files in memory, to have no delay when playing.
I do not know how to do this in the new version, because I do not know how to set the volume back.
You may have a look at our example.
@adrianvintu No need to set audiocache for yourself anymore. Also don't know, why you initialize the audioplayer on every call. You need either a list of audioplayers to play multiple audiosources simultaneously. Or just reuse the player. Just like @jonafeucht said. It's supper simple:
final player= AudioPlayer();
await player.setVolume(0.0);
await player.play(AssetSource('my_asset_file.mp3')); // Or alternatively setSource + resume
await player.stop();
await player.setVolume(1.0);
await player.resume();
Also everthing is explained here: https://github.com/bluefireteam/audioplayers/blob/main/getting_started.md
No need to set audiocache for yourself anymore
- I don't understand, I need to set the prefix: "assets/sounds/"
Also don't know, why you initialize the audioplayer on every call.
- it's because of this instance.setReleaseMode(ReleaseMode.loop);
You did not give any working example on the playListSilently
issue.
Sorry, I cannot see how your comment helps improving the requirements.
class ExampleApp extends StatefulWidget {
const ExampleApp({Key? key}) : super(key: key);
@override
_ExampleAppState createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
List<String> assets = [
'asset1.wav',
'asset2.mp3',
'asset3.wav',
'asset4.mp3',
];
List<AudioPlayer> players = List.generate(4, (_) => AudioPlayer());
static AudioCache audioCache = AudioCache(prefix: 'assets/sounds/');
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
for (var i = 0; i < assets.length; i++) {
final player = players[i];
player.audioCache = audioCache;
await player.setReleaseMode(ReleaseMode.loop);
await player.setSource(AssetSource(assets[i]));
// alternative to playListSilently
await player.setVolume(0);
await player.resume();
await player.stop();
await player.setVolume(1);
}
});
}
@override
void dispose() {
streams.forEach((it) => it.cancel());
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: players
.map(
(p) => ElevatedButton(
onPressed: () {
p.stop();
p.resume();
},
child: Text('Play $p'),
),
)
.toList(),
);
}
}
@adrianvintu was this example sufficient? I think this is a special case, which doesn't need to be provided in the library.
Closing as stale.