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

Audio Player not running in isolates. #292

Open
SyedZeeshanAijaz opened this issue Apr 27, 2024 · 1 comment
Open

Audio Player not running in isolates. #292

SyedZeeshanAijaz opened this issue Apr 27, 2024 · 1 comment
Labels
waiting-for-response Waiting for someone to respond.

Comments

@SyedZeeshanAijaz
Copy link

SyedZeeshanAijaz commented Apr 27, 2024

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):

@DhavalRKansara
Copy link
Collaborator

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?

@DhavalRKansara DhavalRKansara added the waiting-for-response Waiting for someone to respond. label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-for-response Waiting for someone to respond.
Projects
None yet
Development

No branches or pull requests

2 participants