startPlayerFromStream method

Future<void> startPlayerFromStream({
  1. Codec codec = Codec.pcm16,
  2. int numChannels = 1,
  3. int sampleRate = 16000,
  4. int bufferSize = 8192,
  5. TWhenFinished? whenFinished,
})

Used to play something from a Dart stream

This functionnality needs, at least, and Android SDK >= 21

  • The only codec supported is actually Codec.pcm16.
  • The only value possible for numChannels is actually 1.
  • SampleRate is the sample rate of the data you want to play.

Please look to the following notice

Example You can look to the three provided examples :

  • This example shows how to play Live data, with Back Pressure from Flutter Sound
  • This example shows how to play Live data, without Back Pressure from Flutter Sound
  • This example shows how to play some real time sound effects.

Example 1:

await myPlayer.startPlayerFromStream(codec: Codec.pcm16, numChannels: 1, sampleRate: 48000);

await myPlayer.feedFromStream(aBuffer);
await myPlayer.feedFromStream(anotherBuffer);
await myPlayer.feedFromStream(myOtherBuffer);

await myPlayer.stopPlayer();

Example 2:

await myPlayer.startPlayerFromStream(codec: Codec.pcm16, numChannels: 1, sampleRate: 48000);

myPlayer.foodSink.add(FoodData(aBuffer));
myPlayer.foodSink.add(FoodData(anotherBuffer));
myPlayer.foodSink.add(FoodData(myOtherBuffer));

myPlayer.foodSink.add(FoodEvent((){_mPlayer.stopPlayer();}));

Implementation

Future<void> startPlayerFromStream({
  Codec codec = Codec.pcm16,
  int numChannels = 1,
  int sampleRate = 16000,
  int bufferSize = 8192,
  TWhenFinished? whenFinished,
}) async {
  if ((!kIsWeb) &&
      Platform
          .isIOS) // This hack is just to have player to stream working correctly.
  {
    FlutterSoundPlayer player = FlutterSoundPlayer();
    await player.openPlayer();
    Uint8List buf = Uint8List(0);
    //buf.fillRange(0, 1000, 0);
    try {
      await player.startPlayer(
          fromDataBuffer: buf, codec: Codec.pcm16, whenFinished: () {});
    } catch (e) {
      _logger.d('Hacking the bug we have on iOS when playing from stream');
    }
    //await player.stopPlayer();
    /* await */ player.closePlayer();
  }

  await _lock.synchronized(() async {
    await _startPlayerFromStream(
      codec: codec,
      sampleRate: sampleRate,
      numChannels: numChannels,
      bufferSize: bufferSize,
      whenFinished: whenFinished,
    );
  });
}