chipweinberger / flutter_pcm_sound

A flutter plugin for playing raw PCM audio data (16-bit integer)
Other
16 stars 7 forks source link

Using an isolate / backgroung service. #17

Closed saarikivit closed 8 hours ago

saarikivit commented 2 months ago

Hey! I'm very satisfied with this plugin. And I contributed to the melty synth :)

I'm trying to delegate the rendering to an isolate. And that is probably ok. But I'm not able to have Flutter PCM Sound in the isolate. It requires "WidgetsFlutterBinding.ensureInitialized();" which can not be called within an isolate.

My end goal is to make a background player that works with this and melty synth you created. I want to put my phone in the pocket and still be able to listen to the synth. I would also prefer the rendering to happen off main thread.

The following code does not work in an isolate I spawned. It lacks binding.

Future<void> _initializePCM(Synthesizer synth) async {
  FlutterPcmSound.setFeedCallback((rf) => _onFeed(synth, rf));
  FlutterPcmSound.setLogLevel(LogLevel.none);
  await FlutterPcmSound.setFeedThreshold(_thresholdLevel);
  await FlutterPcmSound.setup(sampleRate: _sampleRate, channelCount: 2);
}

It is likely due to the MethodChannel which is unavailable. Would you have any ideas how I could remedy this?

Also any ideas how to implement the audio to be playable in the background is largely helpful. Is that even possible with this?

Thanks a lot for your help!

Best, Tero.

saarikivit commented 2 months ago

Oh and btw. I tried to just delegate only the rendering also to a "compute" method. That resulted in a buzzing sound somehow.

chipweinberger commented 2 months ago

thanks for the issue but i'm not sure!

let me know if you figure it out!

saarikivit commented 3 days ago

I figured it out with a weird method: Using a real background service.

With them I can render the audio in a background thread. The necessary Channel is there.

saarikivit commented 8 hours ago

I'll go ahead and close this.