startPlayerFromTrack method

Future<Duration?> startPlayerFromTrack(
  1. Track track, {
  2. TonSkip? onSkipForward,
  3. TonSkip? onSkipBackward,
  4. TonPaused? onPaused,
  5. TWhenFinished? whenFinished,
  6. Duration? progress,
  7. Duration? duration,
  8. bool? defaultPauseResume,
  9. bool removeUIWhenStopped = true,
})

Play data from a track specification and display controls on the lock screen or an Apple Watch.

The Audio Session must have been open with the parameter withUI.

  • track parameter is a simple structure which describe the sound to play.

    • whenFinished:() : A function for specifying what to do when the playback will be finished.
  • onPaused:() : this parameter can be :

  • a call back function to call when the user hit the Skip Pause button on the lock screen

  • null : The pause button will be handled by Flutter Sound internal

  • onSkipForward:() : this parameter can be :

  • a call back function to call when the user hit the Skip Forward button on the lock screen

  • null : The Skip Forward button will be disabled

  • onSkipBackward:() : this parameter can be :

  • a call back function to call when the user hit the Skip Backward button on the lock screen

  • removeUIWhenStopped : is a boolean to specify if the UI on the lock screen must be removed when the sound is finished or when the App does a stopPlayer(). Most of the time this parameter must be true. It is used only for the rare cases where the App wants to control the lock screen between two playbacks. Be aware that if the UI is not removed, the button Pause/Resume, Skip Backward and Skip Forward remain active between two playbacks. If you want to disable those button, use the API verb nowPlaying(). Remark: actually this parameter is implemented only on iOS.

  • defaultPauseResume : is a boolean value to specify if Flutter Sound must pause/resume the playback by itself when the user hit the pause/resume button. Set this parameter to FALSE if the App wants to manage itself the pause/resume button. If you do not specify this parameter and the onPaused parameter is specified then Flutter Sound will assume FALSE. If you do not specify this parameter and the onPaused parameter is not specified then Flutter Sound will assume TRUE. Remark: actually this parameter is implemented only on iOS.

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

Example:

final fileUri = "https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3";
Track track = Track( codec: Codec.opusOGG, trackPath: fileUri, trackAuthor: '3 Inches of Blood', trackTitle: 'Axes of Evil', albumArtAsset: albumArt )
Duration d = await myPlayer.startPlayerFromTrack
(
track,
whenFinished: ()
{
  logger.d( 'I hope you enjoyed listening to this song' );
},
);

Implementation

Future<Duration?> startPlayerFromTrack(
  Track track, {
  TonSkip? onSkipForward,
  TonSkip? onSkipBackward,
  TonPaused? onPaused,
  TWhenFinished? whenFinished,
  Duration? progress,
  Duration? duration,
  bool? defaultPauseResume,
  bool removeUIWhenStopped = true,
}) async {
  var r = Duration.zero;
  await _lock.synchronized(() async {
    r = await _startPlayerFromTrack(
      track,
      onSkipForward: onSkipForward,
      onSkipBackward: onSkipBackward,
      onPaused: onPaused,
      whenFinished: whenFinished,
      progress: progress,
      duration: duration,
      defaultPauseResume: defaultPauseResume,
      removeUIWhenStopped: removeUIWhenStopped,
    );
  });
  return r;
}