gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 86 forks source link

Is it possible to set the alarm sound to other downloaded audio files besides the ones in assets? #218

Closed wann2 closed 4 months ago

wann2 commented 4 months ago

It looks like the "assetAudioPath" is only set to the audio that is in the asset.

It would be nice to be able to set the alarm sound to other audio file downloaded by the app and stored in the internal folder of getApplicationDocumentsDirectory().

Crash285github commented 4 months ago

i use it like this, and it works well:

assetAudioPath: "((await getApplicationCacheDirectory()).path)/yourFileName.mp3"

should be the same with the documents dir as well :) also if you didnt, you need to add android.permission.READ_EXTERNAL_STORAGE to AndroidManifest for it to work

gdelataillade commented 4 months ago

If you downloaded the file in app documents folder, it should also work with just:

assetAudioPath: 'yourFileName.mp3'
wann2 commented 4 months ago

In case of iOS(iPhone), it seems not work properly.

Unhandled Exception: PlatformException(NATIVE_ERR, [SwiftAlarmPlugin] Failed to load audio for asset: /var/mobile/Containers/Data/Application/E38AF61B-7928-4B53-A167-F6FF3D9/Documents/7051157.m4a, null, null) IOSAlarm.setAlarm (package:alarm/src/ios_alarm.dart:64:7)

gdelataillade commented 4 months ago

Hi @wann2

Did you use getApplicationDocumentsDirectory to build the path ?

wann2 commented 4 months ago

Yes, I used getApplicationDocumentsDirectory, but it seems not work in iPhone. it happens the error.

final Directory directoryDir = await getApplicationDocumentsDirectory(); final alarmSettings = AlarmSettings( id: alarmId, dateTime: DateTime(calcDT.year, calcDT.month, calcDT.day, selectedDT.hour, selectedDT.minute, 0, 0, 0), loopAudio: loopAudio, vibrate: vibrate, volume: volume, assetAudioPath: '${documentsDir.path}/${path.basename(alarmAudio!.path)}', notificationTitle: title, notificationBody: body, enableNotificationOnKill: true, ); await Alarm.set(alarmSettings: alarmSettings) ... Unhandled Exception: PlatformException(NATIVE_ERR, [SwiftAlarmPlugin] Failed to load audio for asset: /var/mobile/Containers/Data/Application/E38AF61B-7928-4B53-A167-F6FF3D9/Documents/7051157.m4a, null, null) IOSAlarm.setAlarm (package:alarm/src/ios_alarm.dart:64:7)

gdelataillade commented 4 months ago

@wann2

Have you tried just passing the filename ? Like I said here:

If you downloaded the file in app documents folder, it should also work with just:

assetAudioPath: 'yourFileName.mp3'
wann2 commented 4 months ago

I just put the music filename in assetAudioPath without the getApplicationDocumentsDirectory path and it works fine. Thank you very much.