Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm creating a music application using Flutter and I'm using the soundpool package. I'm wondering if there's another way to loop the soundtrack other than this:

while (true) {
  soundStreamId = await pool.play(soundId);
  print("Loopin");
  if (status == false) {
    break;
  }
}

By using break, the sound does not stop immediately but it will stop after the track is played completely.

I've tried replacing break with pool.stop(soundStreamId) and pool.pause(soundStreamId) but it gives me this error:

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: 'package:soundpool/soundpool.dart': Failed assertion: line 189 pos 12: 'streamId > 0': Invalid 'streamId' parameter. Only values greater than 0 are valid.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      Soundpool.stop (package:soundpool/soundpool.dart:189:12)
#3      SoundServices.toggleSound (package:Waver/api/sound_service.dart:33:20)
<asynchronous suspension>
#4      _AutoSoundSetterState._toggleSpectrumAnalyzer (package:Waver/Widgets/Setup/AutoSoundSetter.dart:51:20)
#5      _AutoSoundSetterState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:Waver/Widgets/Setup/AutoSoundSetter.dart:211:31)
#6      State.setState (package:flutter/src/widgets/framework.dart:1244:30)
#7      _AutoSoundSetterState.build.<anonymous closure>.<anonymous closure> (package:Waver/Widgets/Setup/AutoSoundSetter.dart:203:27)

Please advise. Thanks!

question from:https://stackoverflow.com/questions/65560249/is-there-a-way-to-loop-a-soundpool-sound-track-flutter-package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
324 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes, I needed a similar thing and found this package :

https://pub.dev/packages/just_audio

It has looping:

Looping and shuffling:

await player.setLoopMode(LoopMode.off); // no looping (default)
await player.setLoopMode(LoopMode.all); // loop playlist
await player.setLoopMode(LoopMode.one); // loop current item
await player.setShuffleModeEnabled(true); // shuffle playlist

Even loop of a playlist:

player.setAudioSource(
  // Loop child 4 times
  LoopingAudioSource(
    count: 4,
    // Play children one after the other
    child: ConcatenatingAudioSource(
      children: [
        // Play a regular media file
        ProgressiveAudioSource(Uri.parse("https://example.com/foo.mp3")),
        // Play a DASH stream
        DashAudioSource(Uri.parse("https://example.com/audio.mdp")),
        // Play an HLS stream
        HlsAudioSource(Uri.parse("https://example.com/audio.m3u8")),
        // Play a segment of the child
        ClippingAudioSource(
          child: ProgressiveAudioSource(Uri.parse("https://w.xyz/p.mp3")),
          start: Duration(seconds: 25),
          end: Duration(seconds: 30),
        ),
      ],
    ),
  ),
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...