Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data flow between audio_service classes and normal classes #359

Closed
gmoreiraa opened this issue Jun 13, 2020 · 3 comments
Closed

Data flow between audio_service classes and normal classes #359

gmoreiraa opened this issue Jun 13, 2020 · 3 comments
Assignees
Labels
1 backlog question Further information is requested

Comments

@gmoreiraa
Copy link

class NormalClass extends StatefulWidget{ ..... }

void _textToSpeechTaskEntrypoint() async {
  AudioServiceBackground.run(() => TextPlayerTask());
}

class _NormalClassNewState extends State<NormalClass>{ 
final StopWatchTimer stopWatch = StopWatchTimer();
...
some stuff  
...

 Button(
         onTap: () async {
          await AudioService.start(
           backgroundTaskEntrypoint: _textToSpeechTaskEntrypoint,
           androidNotificationChannelName: 'Audio Service Demo',
           androidNotificationColor: 0xFF2196f3,
           androidNotificationIcon: 'mipmap/ic_launcher',
           params: {'title' : (widget.isTimer?'Timer':'StopWatch'), 'stopWatch' : stopWatch}
           );
         _playSetState(widget.player);
         },
         type: 'play'),

...
some stuff  
...
 }

class TextPlayerTask extends BackgroundAudioTask { 
...
some stuff  
...  

  @override
  Future<void> onStart(Map<String, dynamic> params) async {
    playPause();
    int second;
    while(true){
      --> second = params['stopWatch'].rawTime.value; <-- THAT LINE THROWS A RUNTIME ERROR
      AudioServiceBackground.setMediaItem(mediaItem(second,params['title']));
      AudioServiceBackground.androidForceEnableMediaButtons();
      _tts.speak('$second');
      
      // Wait for the speech or a pause request.
      await Future.any(
          [Future.delayed(Duration(seconds: 1)), _playPauseFuture()]);
      // If we were just paused...
      if (_playPauseCompleter.isCompleted &&
          !_playing &&
          _processingState != AudioProcessingState.stopped) {
        // Wait to be unpaused...
        await _playPauseFuture();
      }
    }
  }

...
some stuff  
...
}

(The runtime error: Unhandled Exception: Invalid argument: Instance of 'StopWatchTimer').

How can I properly establish communication between these two Class in order to use the same "StopWatchTimer" instance in both???

@gmoreiraa gmoreiraa added 1 backlog question Further information is requested labels Jun 13, 2020
@ryanheise
Copy link
Owner

Hi @G4BR13L-me ,

The FAQ already includes an entry on custom actions, and I have now added an entry on custom events.

I have also added this FAQ:

How do I communicate between the UI and background isolates

As per the README file:

Keep in mind that your Flutter UI and background task run in separate isolates and do not share memory. The only way they communicate is via message passing. Your Flutter UI will only use the AudioService API to communicate with the background task, while your background task will only use the AudioServiceBackground API to interact with the clients, which include the Flutter UI.

There are a number of predefined messages you can pass from the UI to the background isolate via the AudioService API such as play, pause, seekTo, skipToNext, etc. If you want to send another message that is not one of the predefined messages in this API, you can use customAction (See "How do I send a custom action?").

There are also a number of predefined messages you can pass from the background to the UI isolate via the AudioServiceBackground API such as setState, setMediaItem and setQueue. If you want to send another message that is not one of the predefined messages in this API, you can use sendCustomEvent (See "How do I send a custom event?").

@gmoreiraa
Copy link
Author

Thanks @ryanheise , U R MY HEROOO

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 backlog question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants