audio_waveforms
audio_waveforms copied to clipboard
Audio Player not running in isolates.
I am trying to fetch the audio from server and extracting waveform from it in background in an isolate to stop the main UI isolate from blocking.
Code:
Future<void> downloadAudioIsolate(String docsPath) async {
PlayerController controller = PlayerController(); // Initialise
// Extract waveform data
final waveformData = await controller.extractWaveformData(
path: 'path',
noOfSamples: 100,
);
Map<String, dynamic> jsonData = {
"data": waveformData,
};
String jsonString = jsonEncode(jsonData);
controller.dispose();
final dataPath = File("$docsPath${audio.path.split('/').last.split(".mp3").first}.json");
await dataPath.writeAsString(jsonString);
// Write audio to file
await filePath.writeAsBytes(audioResponse.bodyBytes);
}
main->
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocDir = await getApplicationDocumentsDirectory();
// Spawn the isolate and wait for completion
await Isolate.spawn(downloadAudioIsolate, appDocDir.path);
// Continue with your main UI logic
runApp(const MyApp());
}
ERROR:
E/flutter ( 5223): [ERROR:flutter/runtime/dart_isolate.cc(1107)] Unhandled exception: E/flutter ( 5223): 'package:flutter/src/services/platform_channel.dart': Failed assertion: line 554 pos 7: '_binaryMessenger != null || BindingBase.debugBindingType() != null': Cannot set the method call handler before the binary messenger has been initialized. This happens when you call setMethodCallHandler() before the WidgetsFlutterBinding has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() before this or by passing a custom BinaryMessenger instance to MethodChannel(). E/flutter ( 5223): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61) E/flutter ( 5223): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5) E/flutter ( 5223): #2 MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:554:7) E/flutter ( 5223): #3 AudioWaveformsInterface.setMethodCallHandler (package:audio_waveforms/src/base/audio_waveforms_interface.dart:196:20) E/flutter ( 5223): #4 PlatformStreams.init (package:audio_waveforms/src/base/platform_streams.dart:37:44) E/flutter ( 5223): #5 new PlayerController (package:audio_waveforms/src/controllers/player_controller.dart:81:32) E/flutter ( 5223): #6 downloadAudioIsolate (package:isolate_demo/main.dart:48:37) E/flutter ( 5223):
Hello @SyedZeeshanAijaz, I just want to confirm one thing, If we run the above method downloadAudioIsolate as standalone method(without isolate), does it working for you?
Flutter does not support this type of functionality in isolates, and there is already an open issue in the official repo. #119207