From 8e7c876bcd9d2515a45281819a8edca6764db1f6 Mon Sep 17 00:00:00 2001 From: Gautier de Lataillade <32983806+gdelataillade@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:09:30 +0100 Subject: [PATCH] Add iOS asset audio subfolder path --- ios/Classes/SwiftAlarmPlugin.swift | 23 +++++++++++++++-------- lib/model/alarm_settings.dart | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ios/Classes/SwiftAlarmPlugin.swift b/ios/Classes/SwiftAlarmPlugin.swift index ce0161e8..5c671809 100644 --- a/ios/Classes/SwiftAlarmPlugin.swift +++ b/ios/Classes/SwiftAlarmPlugin.swift @@ -116,14 +116,19 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin { } } else { do { - let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] - let filename = String(assetAudio.split(separator: "/").last ?? "") - let assetAudioURL = documentsDirectory.appendingPathComponent(filename) + var assetAudioURL: URL + if assetAudio.starts(with: "/") { + assetAudioURL = URL(fileURLWithPath: assetAudio) + } else { + let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] + let filename = String(assetAudio.split(separator: "/").last ?? "") + assetAudioURL = documentsDirectory.appendingPathComponent(filename) + } let audioPlayer = try AVAudioPlayer(contentsOf: assetAudioURL) self.audioPlayers[id] = audioPlayer } catch { - result(FlutterError.init(code: "NATIVE_ERR", message: "[Alarm] Error loading given local asset path: \(assetAudio)", details: nil)) + result(FlutterError.init(code: "NATIVE_ERR", message: "[Alarm] Error loading audio file: \(assetAudio)", details: nil)) return } } @@ -387,8 +392,9 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin { private func mixOtherAudios() { do { - try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers]) - try AVAudioSession.sharedInstance().setActive(true) + let audioSession = AVAudioSession.sharedInstance() + try audioSession.setCategory(.playback, mode: .default, options: [.mixWithOthers]) + try audioSession.setActive(true) } catch { NSLog("SwiftAlarmPlugin: Error setting up audio session with option mixWithOthers: \(error.localizedDescription)") } @@ -396,8 +402,9 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin { private func duckOtherAudios() { do { - try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.duckOthers]) - try AVAudioSession.sharedInstance().setActive(true) + let audioSession = AVAudioSession.sharedInstance() + try audioSession.setCategory(.playback, mode: .default, options: [.duckOthers]) + try audioSession.setActive(true) } catch { NSLog("SwiftAlarmPlugin: Error setting up audio session with option duckOthers: \(error.localizedDescription)") } diff --git a/lib/model/alarm_settings.dart b/lib/model/alarm_settings.dart index 85c85a37..22185188 100644 --- a/lib/model/alarm_settings.dart +++ b/lib/model/alarm_settings.dart @@ -10,7 +10,7 @@ class AlarmSettings { /// * Project asset: `assets/your_audio.mp3`. /// * Local asset: `/path/to/your/audio.mp3`, which is your `File.path`. /// - /// If you want to use a local asset, you must request android `storage` + /// If you want to use a local asset, you must request android storage /// permission and add the following permission to your `AndroidManifest.xml`: /// `android.permission.READ_EXTERNAL_STORAGE` final String assetAudioPath;