chipweinberger / flutter_pcm_sound

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

flutter_pcm_sound: Play Audio Realtime From Binary Data Stream #1

Closed BinhT1 closed 7 months ago

BinhT1 commented 8 months ago

My mobile app receives binary data stream from server socket and i want to play audio Realtime immediately. If server socket send data in 3 minutes, my mobile app will play audio in 3 minutes too. But the audio depends on sample rate. If sample rate is too high or too low, my mobile app will play audio faster or slower. Faster will get error when the audio cannot feed more data. How can i play audio synchronous with socket server.

Now, my code is like this:

Listen socket and push to queue, 4 first bytes is id so i remove it

super.initState();
FlutterPcmSound.setLogLevel(LogLevel.verbose);
FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
FlutterPcmSound.setFeedThreshold(4000);
FlutterPcmSound.setFeedCallback(onFeed);

_streamSubscription =
    WebsocketRepository().streamBinary.listen((binaryMessage) {
  if (binaryMessage?.length == 4) {
  } else {
    binaryQueue.add(binaryMessage.sublist(4));
  }
});

onFeed function

  void onFeed(int remainingFrames) async {    
    setState(() {    
      _remainingFrames = remainingFrames;   
    });   

    if (stopFeeding == false) {
      if (binaryQueue.isNotEmpty) {
        var frames = binaryQueue.removeFirst();
        await FlutterPcmSound.feed(PcmArrayInt16.fromList(frames));
      } else {
        return;
      }
    }
  }
chipweinberger commented 8 months ago

looks good to me

BinhT1 commented 8 months ago

oh sorry, my problem is the binary data is NOT PCM format, they are binary raw from mp3 file. how can i convert binary data from mp3 file to PCM format?

chipweinberger commented 8 months ago

try asking google