Skip to content

Commit

Permalink
Add convenience function to check if URL is a file or folder and chec…
Browse files Browse the repository at this point in the history
…k playability from there

Signed-off-by: Claudio Cambra <[email protected]>
  • Loading branch information
claucambra committed Jan 17, 2024
1 parent ec8ab24 commit f07c7a3
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Harmony/Common/AudioFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import Foundation
import AVFoundation

enum FilePlayable {
case fileNotPlayable, fileMaybePlayable, filePlayable
}

func playableFileExtensions() -> [String] {
let avTypes = AVURLAsset.audiovisualTypes()
let avExtensions = avTypes
Expand All @@ -20,3 +24,16 @@ func fileHasPlayableExtension(fileURL: URL) -> Bool {
let fileExtension = fileURL.pathExtension.lowercased()
return playableFileExtensions().contains(fileExtension)
}

func filePlayability(fileURL: URL) -> FilePlayable {
guard fileHasPlayableExtension(fileURL: fileURL) else { return .fileNotPlayable }
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
if fileAttributes.isRegularFile! {
return .filePlayable
}
} catch {
print("Error reading file attributes for \(fileURL): \(error).")
}
return .fileMaybePlayable
}

0 comments on commit f07c7a3

Please sign in to comment.