Audio_service: AudioService.start don't do anything

Created on 26 Jun 2020  Â·  8Comments  Â·  Source: ryanheise/audio_service


Describe the bug
After call AudioService.start(backgroundTaskEntrypoint: backgroundTaskEntrypoint); nothing happens.

Minimal reproduction project
I put this FAB and call 'start' method:

FloatingActionButton(
      backgroundColor: mainColor,
      onPressed: start,
      child: Container(
          width: 120,
          margin: EdgeInsets.all(7.5),
          decoration: BoxDecoration(
              shape: BoxShape.circle,
              border: Border.all(color: Color(0xFF055D6F), width: 3.2)),
          child: AnimatedIcon(
            icon: AnimatedIcons.pause_play,
            progress: _animateIcon,
            color: Color(0xFF055D6F),
            size: 35,
          )),
    );

This is the start:

  start() {
    AudioService.start(backgroundTaskEntrypoint: backgroundTaskEntrypoint);
  }

The backgroundTaskEntrypoint is never called. I put breakpoints in code to call. I use the aproach like the example:

void backgroundTaskEntrypoint() async {
  await AudioServiceBackground.run(() => CustomAudioPlayer());
}

and this is the CustomAudioPlayer class

class CustomAudioPlayer extends BackgroundAudioTask {
  final _audioPlayer = AudioPlayer();
  final _completer = Completer();

  @override
  Future<void> onStart(Map<String, dynamic> params) async {
    print("Iniciou o onStart");
    await _audioPlayer.setUrl('http://srv9.abcradio.com.br:7002/listen.mp3');
    _audioPlayer.play();
  }

  @override
  Future<void> onStop() async {
    await _audioPlayer.stop();
    await super.onStop();
  }
}

Error messages
None errors showing in the console or during the build

Expected behavior
Start Playing the Stream

Screenshots
This project is OpenSource. I create a Radio App to help others devs to make yours with the minimal design and usability. I used another version with AudioService and I trying update. The code is in https://github.com/yagoliveira92/radio-basic/tree/develop

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

  • Device: Google Pixel 2 (Genymotion) and Redmi 6A
  • OS: Android 9

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.4, on Linux, locale pt_BR.UTF-8)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 3.6)
[✓] VS Code (version 1.46.1)
[✓] Connected device (1 available)

• No issues found!

Additional context
All content show in my repository: https://github.com/yagoliveira92/radio-basic/tree/develop

1 backlog bug

All 8 comments

I have checked the code, looks like you forgot to connect audio service before start it.

  start() async {
    await AudioService.connect();
    AudioService.start(backgroundTaskEntrypoint: backgroundTaskEntrypoint);
  }

PS Your app is pretty. Love it. Listening the radio when I reply this.

Thanks @stonega! I need some others features to update;
Thanks for review the app =) Look again, I get the media items in iTunes and update the cover using MobX. =D

so is AudioService.connect(); necessary before you do AudioService.start(..);?

I didn't see any mention of it in the docs or in the example project yet it fixed my project too. Might worth to add to docs if it's necessary

From the AudioService documentation:

This class is used from your UI code to establish a connection with the audio service. While connected to the service, your UI may invoke methods of this class to start/pause/stop/etc. playback and listen to changes in playback state and playing media.

So yes, you must establish a connection first, and then while you're connected you can then call the other methods. However:

It is recommended to use AudioServiceWidget to manage this connection automatically.

Met the same, idk, @ryanheise, background task has been started, onStart print message was shown, but notification bar doesn't appear. HomePage has been wrapped by AudioServiceWidget, entrypoint is top-level function, manifest is correct, but notification still doesn't appear

return MaterialApp(
       . . .
        home: audioService.AudioServiceWidget(
            child: HomeMapPage())
        );

I think, that this may be caused, because i call start(*) in another screen, routed by Navigator.materialRoute. I tried to use AudioService.connect(), but it has still no effect

Hi @Gradergage One other thing it could be is that you need to set the state to "playing" before the foreground service is first initiated. You can check the example which does this, and it should work.

Hi @Gradergage One other thing it could be is that you need to set the state to "playing" before the foreground service is first initiated. You can check the example which does this, and it should work.

so before calling AudioService.connect() state should be changed to playing? @ryanheise

No, that comment wasn't about when to call AudioService.connect, rather it was about your background audio task. You need to call setState with a playing parameter of true.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexelisenko picture alexelisenko  Â·  4Comments

mohammadne picture mohammadne  Â·  7Comments

austinbyron picture austinbyron  Â·  4Comments

AhmadAbdollah picture AhmadAbdollah  Â·  6Comments

Okladnoj picture Okladnoj  Â·  5Comments