play method

Future<void> play({
  1. AndroidSound? android,
  2. IosSound? ios,
  3. String? fromAsset,
  4. String? fromFile,
  5. double? volume,
  6. bool? looping,
  7. bool? asAlarm,
})

Plays a sound.

On Android, fromFile is used if set, then fromAsset, then android. On iOS, ios comes first, so a call can play an asset on Android and a built-in sound on iOS, as in the original package. Without android, Android plays the default notification sound.

volume is a linear gain from 0.0 to 1.0, as in the original package. asAlarm plays the sound as an alarm, so it is heard in silent mode.

Implementation

Future<void> play({
  AndroidSound? android,
  IosSound? ios,
  String? fromAsset,
  String? fromFile,
  double? volume,
  bool? looping,
  bool? asAlarm,
}) async {
  final RingtoneSource source;
  if (ios != null && defaultTargetPlatform == TargetPlatform.iOS) {
    source = RingtoneSource.system(
      _ringtoneType(android),
      iosSound: _iosSounds[ios.value],
    );
  } else if (fromFile != null) {
    source = RingtoneSource.file(fromFile);
  } else if (fromAsset != null) {
    source = RingtoneSource.asset(fromAsset);
  } else if (android != null || ios != null) {
    source = RingtoneSource.system(
      _ringtoneType(android),
      iosSound: _iosSounds[ios?.value],
    );
  } else {
    throw ArgumentError('Specify android, ios, fromAsset or fromFile.');
  }
  await _player.play(
    source,
    options: PlaybackOptions(
      volume: volume == null ? 1.0 : math.sqrt(volume),
      looping: looping ?? false,
      usage: asAlarm == true ? SoundUsage.alarm : null,
    ),
  );
}