Skip to content

Commit

Permalink
Add iOS asset audio subfolder path
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Jan 29, 2024
1 parent 292e083 commit 8e7c876
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
23 changes: 15 additions & 8 deletions ios/Classes/SwiftAlarmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -387,17 +392,19 @@ 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)")
}
}

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)")
}
Expand Down
2 changes: 1 addition & 1 deletion lib/model/alarm_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8e7c876

Please sign in to comment.