startPlayer
startPlayer method
Used to play a sound on a player open with openPlayer().
If you are new to Flutter Sound, you may look to this simple guide. If you want to play from a dart stream, use the verb startPlayerFromStream instead of this verb.
When you want to stop the player, you call the verb stopPlayer(). Note : Flutter Sound does not stop automatically the player when finished. If you want this, use the whenFinished parameter.
Parameters
- codec: is the audio and file format of the file/buffer. 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 a format conversion. Please refer to the Codec compatibility Table to know which codecs are currently supported. - One of the two following parameters:
- fromDataBuffer: (if you want to play from a data buffer)
- fromUri: (if you want to play a file or a remote URI). The
fromUri
parameter, if specified, can be one of three possibilities :- The URL of a remote file.
- The path of a local file. Hint: path_provider can be useful if you want to get access to some directories on your device.
- The name of a temporary file (without any slash '/').
- sampleRate: is mandatory with raw PCM codecs (
codec
==Codec.pcm16
orCodec.pcmFloat32
). Not used for other codecs. - numChannels: is only used with raw PCM codecs (
codec
==Codec.pcm16
orCodec.pcmFloat32
). Its value is the number of channels, - whenFinished:() : A function for specifying what to do when the playback will be finished.
Note: You must specify one or the two parameters : fromUri
, fromDataBuffer
.
Return
startPlayer() returns a Duration Future, which is the record duration.
Examples
Example:
Duration d = await myPlayer.startPlayer(
codec: Codec.aacADTS,
fromURI: 'foo'
); // Play a temporary file
_playerSubscription = myPlayer.onProgress.listen((e)
{
// ...
});
myPlayer.setSubscriptionDuration(Duration(milliseconds: 100));
}
Example:
final fileUri = "https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3";
Duration d = await myPlayer.startPlayer // Play a remote file
(
codec: Codec.mp3,
fromURI: fileUri,
whenFinished: ()
{
logger.d( 'I hope you enjoyed listening to this song' );
myPlayer.stopPlayer().then ( (_) { setState((){}); );
},
);
See also
Implementation
Future<Duration?> startPlayer({
Codec codec = Codec.aacADTS,
String? fromURI,
Uint8List? fromDataBuffer,
int sampleRate = 16000, // Used only with PCM codecs
int numChannels = 1, // Used only with PCM codecs
TWhenFinished? whenFinished,
}) async {
Duration? r;
await _lock.synchronized(() async {
r = await _startPlayer(
codec: codec,
fromURI: fromURI,
fromDataBuffer: fromDataBuffer,
sampleRate: sampleRate,
numChannels: numChannels,
whenFinished: whenFinished,
);
}); // timeout: Duration(seconds: 10));
return r;
}