项目作者: inway

项目描述 :
Simple Flutter plugin to play ringtone, alarm & notification sounds
高级语言: Dart
项目地址: git://github.com/inway/flutter_ringtone_player.git
创建时间: 2019-04-26T18:31:23Z
项目社区:https://github.com/inway/flutter_ringtone_player

开源协议:MIT License

下载


flutter_ringtone_player

A simple ringtone, alarm & notification player plugin.

pub package
flutter

Usage

Add following import to your code:

  1. import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';

Then simply call this to play system default notification sound:

  1. FlutterRingtonePlayer().playNotification();

There’s also this generic method allowing you to specify in detail what kind of ringtone should be played:

  1. FlutterRingtonePlayer().play(
  2. android: AndroidSounds.notification,
  3. ios: IosSounds.glass,
  4. looping: true, // Android only - API >= 28
  5. volume: 0.1, // Android only - API >= 28
  6. asAlarm: false, // Android only - all APIs
  7. );

Also you can specify a custom ringtone from assets, or provide direct path to file that works for
both Android and iOS:

  1. FlutterRingtonePlayer().play(fromAsset: "assets/ringtone.wav");
  1. FlutterRingtonePlayer().play(fromFile: "assets/ringtone.wav");

You can specify a platform specific ringtone and it will override the one from assets:

  1. FlutterRingtonePlayer().play(
  2. fromAsset: "assets/ringtone.wav", // will be the sound on Android
  3. ios: IosSounds.glass // will be the sound on iOS
  4. );

.play() optional attributes

Attribute Description
bool looping Enables looping of ringtone. Requires FlutterRingtonePlayer().stop(); to stop ringing.
double volume Sets ringtone volume in range 0 to 1.0.
bool asAlarm Allows to ignore device’s silent/vibration mode and play given sound anyway.

To stop looped ringtone please use:

  1. FlutterRingtonePlayer().stop();

Above works only on Android, and please note that by default Alarm & Ringtone sounds are looped.

Default sounds

Method Android iOS
playAlarm RingtoneManager.TYPE_ALARM IosSounds.alarm
playNotification RingtoneManager.TYPE_NOTIFICATION IosSounds.triTone
playRingtone RingtoneManager.TYPE_RINGTONE IosSounds.electronic

Note on iOS sounds

If you want to use any other sound on iOS you can always specify a valid Sound ID and manually construct [IosSound]:

  1. FlutterRingtonePlayer().play(
  2. android: AndroidSounds.notification,
  3. ios: const IosSound(1023),
  4. looping: true,
  5. volume: 0.1,
  6. );