# feedInt16FromStream method

Future<int> feedInt16FromStream(

1. List<Int16List> buffer

)

Feed a Int16 stream not interleaved when a flow control is wanted

Please look to this small guide if you need help.

## Parameters

- buffer: is a List of Int16List containing the audio data that you want to play. The List length must be equal to the number of channels. All the Int16List length MUST be same and contain your data.

## Return

Returns a Future that you MUST await if you really need a flow control. This future is declared completed when the data has been played, or at least when they had be given to the lower layer of the software. Note: don't use the `int` returned. It is just for legacy reason and must not be used.

## Example

    await myPlayer.startPlayerFromStream
    (
        codec: Codec.pcm16
        numChannels: 2
        sampleRate: 48100
        interleaved: false,
    );

    await myPlayer.feedInt16FromStream(myData);

## See also

- feedF32FromStream()
- feedUint8FromStream()
- float32Sink
- int16Sink
- uint8ListSink
- Please look to this small guide if you need help

## Implementation

    Future<int> feedInt16FromStream(List<Int16List> buffer) async {
      await _waitOpen();
      if (_isInited != Initialized.fullyInitialized) {
        throw Exception('Player is not open');
      }
      if (isStopped) {
        return 0;
      }

      if (codec != Codec.pcm16) {
        logger.e('Cannot feed with Int16 on a Codec <> pcm16');
        throw Exception('Cannot feed with Int16 on a Codec <> pcm16');
      }
      if (interleaved) {
        logger.e('Cannot feed with Int16 with interleaved mode');
        throw Exception('Cannot feed with Int16 with interleaved mode');
      }
      if (buffer.length != numChannels) {
        logger.e(
          'feedInt16FromStream() : buffer length (${buffer.length}) != the number of channels ($numChannels)',
        );
        throw Exception(
          'feedInt16FromStream() : buffer length (${buffer.length}) != the number of channels ($numChannels)',
        );
      }
      for (int channel = 1; channel < numChannels; ++channel) {
        if (buffer[channel].length != buffer[0].length) {
          logger.e(
            'feedInt16FromStream() : buffer length[0] (${buffer[0].length}) != the number of channels ($numChannels)',
          );
          throw Exception(
            'feedInt16FromStream() : buffer length[0] (${buffer.length}) != buffer[$channel].length (${buffer[channel].length})',
          );
        }
      }

      _needSomeFoodCompleter =
          Completer<int>(); // Not completed until the device accept new data
      try {
        var ln = await (FlutterSoundPlayerPlatform.instance.feedInt16(
          this,
          data: buffer,
        ));
        assert(ln >= 0); // feedFromStream() is not happy if < 0
        if (ln != 0) {
          // If the device accepted some data, then no need to wait
          // It is the tau_core responsability to send a "needSomeFood" then it is again available for new data
          _needSomeFoodCompleter = null;
          return (ln);
        } else {
          //logger.i("The device has enough data");
        }
      } on Exception {
        _needSomeFoodCompleter = null;
        if (isStopped) {
          return 0;
        }
        rethrow;
      }

      if (_needSomeFoodCompleter != null) {
        return _needSomeFoodCompleter!.future;
      }
      return 0;
    }