Audio_service: AudioService.positionStream cannot reset when current music played done if repeat mode is loop one

Created on 27 Oct 2020  Â·  11Comments  Â·  Source: ryanheise/audio_service

Which API doesn't behave as documented, and how does it misbehave?

when I set repeat mode is one, the current music played done, AudioService.positionStream cannot reset to zero.

or cannot use the API to build the processbar?

Minimal reproduction project
Provide a link here using one of two options:


    _audioPlayer.setLoopMode(LoopMode.one);
    _audioPlayer.setShuffleModeEnabled(false);


class ProcessBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<MediaItem>(
      stream: AudioService.currentMediaItemStream,
      builder: (context, snapshot) {
        final duration = snapshot.data?.duration ?? Duration.zero;
        return StreamBuilder<Duration>(
          stream: AudioService.positionStream,
          builder: (context, snapshot) {
            var position = snapshot.data ?? Duration.zero;
            if (position > duration) {
              position = duration;
            }
            return SeekBar(
              duration: duration,
              position: position,
              onChangeEnd: (newPosition) {
                AudioService.seekTo(newPosition);
              },
            );
          },
        );
      },
    );
  }
}

To Reproduce (i.e. user steps, not code)
Steps to reproduce the behavior:

  1. Go to 'Home Page'
  2. Switch 'repeat mode' to repeat one
  3. played music done
  4. prcessbar is always in the end of position, processbar cannot update any more

Error messages

NA

Expected behavior
Switch 'repeat mode' to repeat one, played music done, prcessbar can reset to start position.

Screenshots
If applicable, add screenshots to help explain your problem.

Runtime Environment (please complete the following information if relevant):

  • Device: MI 6
  • OS: Android 9

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.2, on Microsoft Windows [Version 10.0.19041.572], locale zh-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[!] Android Studio (version 4.1.0)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code, 64-bit edition (version 1.50.1)
[✓] Connected device (1 available)

Additional context

1 backlog bug

All 11 comments

Thanks for submitting a bug report, but I will need you to fill in all sections, and provide a link to a minimal reproduction project that I can clone and run.

@ryanheise I share my project: https://github.com/zengkid/plexmusic, please help to take a look if you feel free.

Can you prepare a more simple reproduction project? (i.e. "minimal"?)

@ryanheise already minimal the project. please check again. thanks

You haven't filled in all sections of the bug report yet, and I would appreciate if you can fork this project and modify the example to reproduce the bug as mentioned in the instructions. Your project is unfamiliar to me and it would take me time to navigate all of the different Dart files, and I do not want to spend time doing that. I would really appreciate it if you can please complete all sections in the bug report as per the instructions, thanks. I will then be able to look at the git diffs to see what lines you have changed in order to reproduce the bug.

@ryanheise I have update some sections.
the project is copy from just_audio example project. I just move some code to standalone file, and then update to support background play.

I will look at it on the weekend at the earliest (mainly because your project is unfamiliar to me.)

@ryanheise many thanks, I am a newbie for flutter.

Hello @zengkid, how do u access positionStream value ? This getter doesn't exist for me... I can access AudioService.currentMediaItemStream or AudioService.playbackStateStream in order to catch a non stream value, currentPosition...

To get it, i Have to workaround by doing nested Stream builder, the second one use a Stream.periodic to get the parent currentPosition value.......

return StreamBuilder<PlaybackState>(
      stream: AudioService.playbackStateStream,
      builder: (_, snapshot) {
        if (!snapshot.hasData) return SizedBox();
        return StreamBuilder<Duration>(
          stream: Stream<Duration>.periodic(Duration(seconds: 1)),
          builder: (_, durationSnapshot) {
            Duration position = snapshot.data.currentPosition;
            return Column(
              children: [
                SleekCircularSlider(
                  onChangeStart: (double value) {
                    if (AudioService.playbackState.playing) {
                      sl.get<PlayerCubit>().playPause();
                    }
                  },
                  onChangeEnd: (double value) {
                    Duration newPosition = Duration(seconds: value.toInt());
                    sl.get<PlayerCubit>().seekTo(newPosition);
                    sl.get<PlayerCubit>().playPause();
                  },
                  min: 0,
                  max: metadata.duration.inSeconds.toDouble(),
                  initialValue: position.inSeconds
                      .toDouble(), //positionMusic.inSeconds.toDouble(),
                  innerWidget: (val) {
                    return Padding(
                      padding: EdgeInsets.all(horizontalPadding * 1.7),
                      child: CustomCircleAvatar(
                        radius: width * 0.5,
                        image: NetworkImage(metadata.artUri, scale: 1.0),
                      ),
                    );
                  },
                  appearance: CircularSliderAppearance(
                    animationEnabled: false,
                    angleRange: 80,
                    startAngle: 130,
                    counterClockwise: true,
                    size: height * 0.375,
                    customWidths: CustomSliderWidths(
                      handlerSize: 6,
                      progressBarWidth: 1,
                      shadowWidth: 1,
                      trackWidth: 1,
                    ),
                    customColors: CustomSliderColors(
                      dotColor: k_goldColor,
                      trackColor: Color(0xFF7A7A7A),
                      progressBarColor: k_goldColor,
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(
                    top: horizontalPadding,
                  ),
                  child: Text(
                    '${twoDigits(position.inMinutes)}:${twoDigits(position.inSeconds.remainder(60))}',
                    style: TextStyle(
                      color: k_goldColor,
                      fontSize: 12,
                    ),
                  ),
                ),
              ],
            );
          },
        );
      },
    );

This feature is on git master. It's one of the features I'm currently testing before rolling out the next release.

@zengkid Here is how to create a minimal reproduction project:

  1. Fork this repository
  2. Modify the example in a "minimal" way to reproduce the bug
  3. Provide a link

E.g. add this one line to the official example's onStart method:

    _player.setLoopMode(LoopMode.one); // this is truly minimal

When I did this myself, everything worked as expected, so the bug is likely to be in your app rather than in this plugin.

As such, I'll close this bug report. If you still believe the bug is actually in audio_service, you will need to do more work to demonstrate that, after which I'll reopen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Divyanshu133 picture Divyanshu133  Â·  5Comments

pblinux picture pblinux  Â·  5Comments

ryanheise picture ryanheise  Â·  6Comments

hemalmoradiya picture hemalmoradiya  Â·  4Comments

austinbyron picture austinbyron  Â·  4Comments