Skip to content

Commit

Permalink
fix: ios local file path
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Aug 27, 2024
1 parent 10e8c3a commit 7d1a226
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
37 changes: 29 additions & 8 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
let channels: Int?
let volume: Float?
let delay: Float?
var isLocalUrl: Bool = call.getBool("isUrl") ?? false // Existing flag for local URLs
var isLocalUrl: Bool = call.getBool("isUrl") ?? false

if audioId != "" {
var assetPath: String = call.getString(Constant.AssetPathKey) ?? ""
Expand Down Expand Up @@ -324,33 +324,54 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
self.audioList[audioId] = remoteAudioAsset
call.resolve()
} else if isLocalUrl == false {
// if assetPath dont start with public/ add it
// Handle public folder
// if assetPath doesnt start with public/ add it
assetPath = assetPath.starts(with: "public/") ? assetPath : "public/" + assetPath

let assetPathSplit = assetPath.components(separatedBy: ".")
basePath = Bundle.main.path(forResource: assetPathSplit[0], ofType: assetPathSplit[1])
} else {
let url = URL(string: assetPath)
basePath = url!.path
// Handle local file URL
let fileURL = URL(fileURLWithPath: assetPath)
basePath = fileURL.path
}

if FileManager.default.fileExists(atPath: basePath ?? "") {
if let basePath = basePath, FileManager.default.fileExists(atPath: basePath) {
if !complex {
let soundFileUrl = URL(fileURLWithPath: basePath ?? "")
let soundFileUrl = URL(fileURLWithPath: basePath)
var soundId = SystemSoundID()
AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
self.audioList[audioId] = NSNumber(value: Int32(soundId))
call.resolve()
} else {
let audioAsset: AudioAsset = AudioAsset(
let audioAsset = AudioAsset(
owner: self,
withAssetId: audioId, withPath: basePath, withChannels: channels,
withVolume: volume, withFadeDelay: delay)
self.audioList[audioId] = audioAsset
call.resolve()
}
} else {
call.reject(Constant.ErrorAssetPath + " - " + assetPath)
if FileManager.default.fileExists(atPath: assetPath) {
// Use the original assetPath
if !complex {
let soundFileUrl = URL(fileURLWithPath: assetPath)
var soundId = SystemSoundID()
AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
self.audioList[audioId] = NSNumber(value: Int32(soundId))
call.resolve()
} else {
let audioAsset = AudioAsset(
owner: self,
withAssetId: audioId, withPath: assetPath, withChannels: channels,
withVolume: volume, withFadeDelay: delay)
self.audioList[audioId] = audioAsset
call.resolve()
}
} else {
let attributes = try? FileManager.default.attributesOfItem(atPath: assetPath)
call.reject(Constant.ErrorAssetPath + " - " + assetPath)
}
}
} else {
call.reject(Constant.ErrorAssetAlreadyLoaded + " - " + audioId)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@
},
"publishConfig": {
"access": "public"
}
},
"packageManager": "pnpm@9.7.0+sha512.dc09430156b427f5ecfc79888899e1c39d2d690f004be70e05230b72cb173d96839587545d09429b55ac3c429c801b4dc3c0e002f653830a420fa2dd4e3cf9cf"
}

0 comments on commit 7d1a226

Please sign in to comment.