arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

The Ï„ Player API

startPlayer().

hashtag
startPlayer()

  • Dart API: startPlayer()arrow-up-right.

You can use startPlayer to play a sound.

  • startPlayer() has three optional parameters, depending on your sound source :

    • fromUri: (if you want to play a file or a remote URI)

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

  • You use the optional parametercodec: for specifying the audio and file format of the file. Please refer to the 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.

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

Example:

</div>

Example:

</div>

fromDataBuffer:
(if you want to play from a data buffer)
  • sampleRate is mandatory if codec == Codec.pcm16. Not used for other codecs.

  • Codec compatibility Tablearrow-up-right
    path_providerarrow-up-right
    Dart
    Javascript
    Dart
    Javascript
    
            Directory tempDir = await getTemporaryDirectory();
            File fin = await File ('${tempDir.path}/flutter_sound-tmp.aac');
            Duration d = await myPlayer.startPlayer(fin.path, codec: Codec.aacADTS);
    
            _playerSubscription = myPlayer.onProgress.listen((e)
            {
                    // ...
            });
    
            Lorem ipsum ...
    
        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: ()
                    {
                             print( 'I hope you enjoyed listening to this song' );
                    },
        );
    
            Lorem ipsum ...