我一直在尝试快速找到如何在macOS上使用UTI(统一类型标识符),但是却找不到任何东西。有谁知道我该如何使用UTI在AVPlayer可以播放的目录中获取视频文件列表?
使用加藤裕树的UTIKit(https://github.com/cockscomb/UTIKit):
let dir = URL(fileURLWithPath: "./your_dir_here")
let mimeTypes = AVURLAsset.audiovisualMIMETypes()
let playableUtis = mimeTypes.map { UTI(mimeType: $0)! }
let dirContents = try! FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles])
let movies = dirContents.filter { UTI("public.movie") ~= UTI(filenameExtension: $0.pathExtension) }
let playableMovies = movies.filter {
guard let movieUti = UTI(filenameExtension: $0.pathExtension) else {
return false
}
return playableUtis.contains(movieUti)
}
现在playableMovies
仅包含AVPlayer
可以播放的电影文件。