audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

working minimal examples

Open visign3d opened this issue 2 years ago • 3 comments

Can you please provide some working exaples of playing local assets ?

visign3d avatar Jul 31 '22 09:07 visign3d

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.

adrianvintu avatar Aug 02 '22 09:08 adrianvintu

.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

jonafeucht avatar Aug 03 '22 07:08 jonafeucht

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.

adrianvintu avatar Aug 03 '22 19:08 adrianvintu

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

Gustl22 avatar Sep 27 '22 20:09 Gustl22

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.

adrianvintu avatar Sep 27 '22 20:09 adrianvintu

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(),
    );
  }
}

Gustl22 avatar Sep 27 '22 23:09 Gustl22

@adrianvintu was this example sufficient? I think this is a special case, which doesn't need to be provided in the library.

Gustl22 avatar Oct 08 '22 18:10 Gustl22

Closing as stale.

Gustl22 avatar Nov 03 '22 09:11 Gustl22