thyagoluciano / flutter_radio

Radio station streaming example with ExoPlayer and Flutter.
GNU General Public License v3.0
86 stars 41 forks source link

Keep playing in background #5

Closed incafox closed 5 years ago

incafox commented 5 years ago

Hey, i was trying to use this plugin to play in background, but i dont know how to that with flutter and dart, do you know how to use this plugin as an audio service? Do i need to tweak the native implementation in order to use the player as a service? Please help, im stuck 2 days in this problem.

incafox commented 5 years ago

https://medium.com/flutter-io/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124

yagoliveira92 commented 5 years ago

@incafox I recommend you try using audio_service plugin together with the flutter_radio. The audio_service make audio in background. I will try, but today I work in another project.

incafox commented 5 years ago

@yagoliveira92 i tried that ..., dont waste your time, it doesn't work so i maked my own background service with java and exoplayer.

yagoliveira92 commented 5 years ago

Hi @incafox, I got using flutter_radio in audio_service. If you still looking for help, send a message to me. =D

Basically, i put this code for audio_service class

void _backgroundAudioPlayerTask() async {
  CustomAudioPlayer player = CustomAudioPlayer();
  AudioServiceBackground.run(
    onStart: player.run,
    onPlay: player.play,
    onPause: player.pause,
    onStop: player.stop,
    onClick: (MediaButton button) => player.playPause(),
  );
}

class CustomAudioPlayer {

  bool _playing;
  Completer _completer = Completer();

  Future<void> run() async {
    MediaItem mediaItem = MediaItem(
        id: 'audio_1', 
        album: 'ABC Radio', 
        title: 'A rádio que não cansa vc');
    AudioServiceBackground.setMediaItem(mediaItem);
    audioStart();
    play();
    await _completer.future;
  }

  Future<void> audioStart() async {
    await FlutterRadio.audioStart();
    print('Audio Start OK');
  }

  void playPause() {
    if (_playing)
      pause();
    else
      play();
  }

  void play() {
    FlutterRadio.play(url: streamUrl);
    _playing = true;
    AudioServiceBackground.setState(
        controls: [pauseControl, stopControl],
        basicState: BasicPlaybackState.playing);
    buttonState = !buttonState;
  }

  void pause() {
    FlutterRadio.playOrPause(url: streamUrl);
    AudioServiceBackground.setState(
        controls: [playControl, stopControl],
        basicState: BasicPlaybackState.paused);
  }

  void stop() {
    FlutterRadio.playOrPause(url: streamUrl);
    AudioServiceBackground.setState(
        controls: [], basicState: BasicPlaybackState.stopped);
    buttonState = !buttonState;
  }
}
incafox commented 5 years ago

@yagoliveira92 great job! looks like i wasted my time learning java and custom android services. lol

hrjt commented 5 years ago

@yagoliveira92 can you please post complete code for this dart. i am having issues.

odrevet commented 5 years ago

@hrjt I use flutter_radio with audio_service, the complete source code that you are seeking for is https://github.com/odrevet/bide-et-musique-flutter/blob/master/lib/player.dart

egoan82 commented 4 years ago

Good day to all,

Companion @yagoliveira92 , I see that you managed to solve the problem with Flutter_Radio, it is possible that you share the code I have problems using the two plugins.

Thank you for your kind collaboration.

wgerez commented 3 years ago

@yagoliveira92 can you please post complete code for this dart. Thank you!.

yagoliveira92 commented 3 years ago

@wgerez

My project is open:

https://github.com/yagoliveira92/radio-basic

Enjoy =D