open method

Future<void> open(
  1. Playable playable, {
  2. bool autoStart = _DEFAULT_AUTO_START,
  3. double? volume,
  4. bool respectSilentMode = _DEFAULT_RESPECT_SILENT_MODE,
  5. bool showNotification = _DEFAULT_SHOW_NOTIFICATION,
  6. Duration? seek,
  7. double? playSpeed,
  8. double? pitch,
  9. NotificationSettings? notificationSettings,
  10. LoopMode loopMode = _DEFAULT_LOOP_MODE,
  11. PlayInBackground playInBackground = _DEFAULT_PLAY_IN_BACKGROUND,
  12. HeadPhoneStrategy headPhoneStrategy = _DEFAULT_HEADPHONE_STRATEGY,
  13. AudioFocusStrategy? audioFocusStrategy,
  14. bool forceOpen = false,
})

Open a song from the asset

Example

AssetsAudioPlayer _assetsAudioPlayer = AssetsAudioPlayer();

_assetsAudioPlayer.open(Audio('assets/audios/song1.mp3'))

Don't forget to declare the audio folder in your pubspec.yaml

flutter:
  assets:
    - assets/audios/

Implementation

Future<void> open(
  Playable playable, {
  bool autoStart = _DEFAULT_AUTO_START,
  double? volume,
  bool respectSilentMode = _DEFAULT_RESPECT_SILENT_MODE,
  bool showNotification = _DEFAULT_SHOW_NOTIFICATION,
  Duration? seek,
  double? playSpeed,
  double? pitch,
  NotificationSettings? notificationSettings,
  LoopMode loopMode = _DEFAULT_LOOP_MODE,
  PlayInBackground playInBackground = _DEFAULT_PLAY_IN_BACKGROUND,
  HeadPhoneStrategy headPhoneStrategy = _DEFAULT_HEADPHONE_STRATEGY,
  AudioFocusStrategy? audioFocusStrategy,
  bool forceOpen = false, // skip the _acceptUserOpen
}) async {
  final focusStrategy = audioFocusStrategy ?? defaultFocusStrategy;

  if (forceOpen) {
    _acceptUserOpen = true;
  }

  if (_acceptUserOpen == false) {
    return;
  }

  try {
    _acceptUserOpen = false;
    Playlist? playlist;
    if (playable is Playlist && playable.audios.isNotEmpty) {
      playlist = playable;
    } else if (playable is Audio) {
      playlist = Playlist(audios: [playable]);
    }

    if (playlist != null) {
      await _openPlaylist(
        playlist,
        autoStart: autoStart,
        volume: volume,
        respectSilentMode: respectSilentMode,
        showNotification: showNotification,
        seek: seek,
        loopMode: loopMode,
        playSpeed: playSpeed,
        pitch: pitch,
        headPhoneStrategy: headPhoneStrategy,
        audioFocusStrategy: focusStrategy,
        notificationSettings:
            notificationSettings ?? defaultNotificationSettings,
        playInBackground: playInBackground,
      );
    }
    _acceptUserOpen = true;
  } catch (t) {
    _acceptUserOpen = true;
    rethrow;
  }
}