open method
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,
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;
}
}