Skip to main content Link Menu Expand (external link) Document Search Copy Copied
startPlayer method - FlutterSoundPlayer class - player library - Dart API
menu
startPlayer

startPlayer method

Future<Duration?> startPlayer({
  1. Codec codec = Codec.aacADTS,
  2. int sampleRate = 16000,
  3. int numChannels = 1,
  4. String? fromURI,
  5. Uint8List? fromDataBuffer,
  6. TWhenFinished? whenFinished,
})

Used to play a sound.


  • startPlayer() has four optional parameters, depending on your sound source :
    • fromUri: (if you want to play a file or a remote URI)
    • fromDataBuffer: (if you want to play from a data buffer)
    • sampleRate is mandatory if codec == Codec.pcm16 or Codec.pcmFloat32. Not used for other codecs.
    • numChannels is optional and is only used with raw PCM codecs. Its value is the number of channels,

You must specify one or the two parameters : fromUri, fromDataBuffer.

  • You use the optional parametercodec: for specifying the audio and file format of the file. Please refer to the Codec compatibility Table to know which codecs are currently supported.

  • whenFinished:() : A lambda function for specifying what to do when the playback will be finished.

Very often, the codec: parameter is not useful. Flutter Sound will adapt itself depending on the real format of the file provided. But this parameter is necessary when Flutter Sound must do format conversion (for example to play opusOGG on iOS).

startPlayer() returns a Duration Future, which is the record duration.

The fromUri parameter, if specified, can be one of three posibilities :

  • The URL of a remote file
  • The path of a local file
  • The name of a temporary file (without any slash '/')

Hint: path_provider can be useful if you want to get access to some directories on your device.

Example:

        Duration d = await myPlayer.startPlayer(fromURI: 'foo', codec: Codec.aacADTS); // Play a temporary file

        _playerSubscription = myPlayer.onProgress.listen((e)
        {
                // ...
        });
}

Example:

    final fileUri = "https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3";

    Duration d = await myPlayer.startPlayer
    (
                fromURI: fileUri,
                codec: Codec.mp3,
                whenFinished: ()
                {
                         logger.d( 'I hope you enjoyed listening to this song' );
                },
    );

Implementation

Future<Duration?> startPlayer({
  Codec codec = Codec.aacADTS,
  int sampleRate = 16000, // Used only with PCM codec
  int numChannels = 1, // Used only with codec == Codec.pcm16
  String? fromURI,
  Uint8List? fromDataBuffer,
  TWhenFinished? whenFinished,
}) async {
  Duration? r;
  await _lock.synchronized(
    () async {
      //!!!  try {
      r = await _startPlayer(
        fromURI: fromURI,
        fromDataBuffer: fromDataBuffer,
        codec: codec,
        sampleRate: sampleRate,
        numChannels: numChannels,
        whenFinished: whenFinished,
      );
      //!!!}
      //!!!  on Exception {
      //r = Duration.zero;
      //!!!    await _stop();
      //!!!    rethrow;
      //!!!  };
    },
  ); // timeout: Duration(seconds: 10));
  return r;
}
flutter_sound 9.24.0