使用记录的音频安排 Flutter 通知

huangapple go评论71阅读模式
英文:

Flutter schedule notification using recorded audio

问题

目前,我正在构建一个需要我实现录制音频并将录制的音频用作本地通知声音的应用程序。

我已经使用 flutter_local_notifications 设置了通知,但据我所知,使用这个插件,你只能在运行前将声音添加到资源文件夹。

这会像这样:

const String soundName = 'test_sound.mp3';

NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        channelDescription: 'channel description',
        sound: RawResourceAndroidNotificationSound(soundName.split('.').first),
      ),
      iOS: DarwinNotificationDetails(),
    );

是否有一种方法在运行时录制音频文件并在安排新通知时立即使用它们?

英文:

Currently I am building an app that requires me to implement a functionality to record audio and use the recorded audio as the sound for a local notification.

I have already set up the notifications using flutter_local_notifications, but as far as I am aware, with this plugin you can only specify sounds that have been added to the resource folder before runtime.

This would look like this:

const String soundName = 'test_sound.mp3';

NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        channelDescription: 'channel description',
        sound: RawResourceAndroidNotificationSound(soundName.split('.').first),
      ),
      iOS: DarwinNotificationDetails(),
    );

Is there a way to record audio files during runtime and use them right away when scheduling a new notification?

答案1

得分: 0

解决方案是录制音频文件并将其保存到应用程序之外的位置。例如,可以这样做:

// Android
var audio_directory = '/sdcard/Android/media/your_app_name/audio';

// iOS
final Directory dir = await getLibraryDirectory();
var audio_directory = '${join(dir.path, 'Sounds')}/';

然后在安排新通知时使用该文件,如下所示:

NotificationDetails(
      android: AndroidNotificationDetails(
        '1',
        '1',
        channelDescription: '1',
        importance: Importance.max,
        sound: UriAndroidNotificationSound(
                'file:///sdcard/Android/media/your_app_name/$soundFile.wav',
               ),
        playSound: vibrationOnly ? false : true,
      ),
      iOS: DarwinNotificationDetails(
              sound: '$soundFile.aiff',
              presentSound: true,
           ),
);
英文:

The solution is to record the audio file and save it to a location outside of the app. This could, for example, be:

// Android
var audio_directory = '/sdcard/Android/media/your_app_name/audio';

// iOS
final Directory dir = await getLibraryDirectory();
var audio_directory = '${join(dir.path, 'Sounds')}/';

Then use the file when scheduling a new notification like this:

NotificationDetails(
      android: AndroidNotificationDetails(
        '1',
        '1',
        channelDescription: '1',
        importance: Importance.max,
        sound: UriAndroidNotificationSound(
                'file:///sdcard/Android/media/your_app_name/$soundFile.wav',
               ),
        playSound: vibrationOnly ? false : true,
      ),
      iOS: DarwinNotificationDetails(
              sound: '$soundFile.aiff',
              presentSound: true,
           ),
);

huangapple
  • 本文由 发表于 2023年2月18日 02:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定