ryanheise / just_audio

Audio Player
1.03k stars 647 forks source link

Local Streaming audio is not working in the ios device #1231

Open srinivas1100 opened 4 months ago

srinivas1100 commented 4 months ago

Which API doesn't behave as documented, and how does it misbehave? I'm using the just_audio package to generate audio from OpenAI and play it. I'm utilizing the http StreamedResponse to generate the audio. After receiving a successful response, I open the HttpServer and bind the response to this server. The code details are provided below. While this setup works fine on Android, it's not functioning properly on iOS.

Minimal reproduction project Provide a link here using one of two options:

  1. Fork this repository and add the api key https://github.com/srinivas1100/bug-report-audio-package.git

    class OpenAiApi {
    final String _baseUrl = 'https://api.openai.com/v1/audio/speech';
    final String _apiKey;
    AudioPlayer audioPlayer = AudioPlayer();
    OpenAiApi(this._apiKey);
    Future<void> getSpeech(String model, String input, String voice) async {
    log("GET SPEECH IS CALLED");
    final client = http.Client();
    final request = http.Request('POST', Uri.parse(_baseUrl))
      ..headers['Authorization'] = 'Bearer $_apiKey'
      ..headers['Content-Type'] = 'application/json'
      ..body = jsonEncode(<String, String>{
        'model': model,
        'input': input,
        'voice': voice,
      });
    
    StreamedResponse streamedResponse = await client.send(request);
    
    // streamedResponse.stream.listen((data) {
    //   log("STREAM DATA : $data");
    // });
    log("STREAM REQUEST IS SENT");
    
    HttpServer server =
        await HttpServer.bind(InternetAddress.loopbackIPv4, 8080, shared: true);
    
    try {
      server.listen(
        (HttpRequest serverRequest) async =>
            await streamedResponse.stream.pipe(serverRequest.response),
      );
    } catch (e) {
      log(e.toString());
    }
    
    log("PORT IS LISTENING ${server.port}");
    
    // log("STREAM LENGTH : ${streamedResponse.contentLength}");
    
    await audioPlayer.setUrl('http://localhost:${server.port}');
    
    // await audioPlayer.load();
    
    await audioPlayer.play();
    
    log("AUDIO PLAYER LENGTH : ${audioPlayer.durationStream}");
    
    log("AUDIO IS PLAYING");
    }
    }

    To Reproduce (i.e. user steps, not code) Steps to reproduce the behavior:

  2. Fork the code in this url https://github.com/srinivas1100/bug-report-audio-package.git
  3. launch any ios device
  4. run any real ios device or simulator
  5. click the button it will get the error

Error messages

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: (-11850) Operation Stopped
#0      AudioPlayer._load (package:just_audio/just_audio.dart:869:9)
<asynchronous suspension>
#1      AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1465:28)
<asynchronous suspension>

Expected behavior Once the API returns the streaming response, I bind it to a local server. Then, I use the URL of that local server with the audio player, which automatically plays the audio.

Screenshots If applicable, No

Desktop (please complete the following information):

Smartphone (please complete the following information):

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.3 23D56 darwin-arm64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.88.1)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

Additional context This bug is effect my complere application all ios users are unable to play the audio it is critical issue

Omu0503 commented 3 months ago

Hi! were you able to get over this problem? im facing the same issue

maeda-takuyaa commented 3 months ago

I have the same problem as you, is there a way to fix this error? Thanks

willsmanley commented 2 months ago

I'm experiencing the same issue. I also created a minimal reproducible example here: https://github.com/willsmanley/audio-test

This example connects to a websocket server, receives Uint8List bytes, but cant play audio from the stream and eventually results in: flutter: Error in playing stream: (-11800) The operation could not be completed flutter: Error in playing stream: MissingPluginException(No implementation found for method load on channel com.ryanheise.just_audio.methods.91d20774-96b0-4621-9a66-8aebce1776d5)

I have the correct ios permissions and flutter clean.

Thank you in advance for any support!

colarking commented 1 month ago

I have the same error like, my audiobytes. from websocket too, and define a new class extend StreamAudioSource, but it print Source error Exception;

1318