I want to import video files with NSOpenPanel. How can I do it?
I want play this video on a avkit player. Maybe you could help with playing the video file as well?
This is the code I wrote
let filePicker: NSOpenPanel = NSOpenPanel()
filePicker.allowsMultipleSelection = false
filePicker.canChooseFiles = true
filePicker.canChooseDirectories = false
filePicker.appearance = NSAppearance(named: .aqua)
filePicker.styleMask.insert(.unifiedTitleAndToolbar)
filePicker.styleMask.insert(.titled)
filePicker.titlebarAppearsTransparent = true
filePicker.runModal()
let chosenFile = filePicker.url
if chosenFile != nil
{
let imageImport = NSImage(contentsOf: chosenFile!)
}
I tried to find tutorials about it, but it seems like this isn't a common problem.
Use the allowedFileTypes
property, along with the Uniform Type Identifiers system.
If you want to filter for any files that Mac OS X consider a movie file, regardless of extension, pass in public.movie
:
filePicker.allowedFileTypes = ["public.movie"]
If you want to filter for only MPEG-4 files, a specific movie format, pass in a different UTI:
filePicker.allowedFileTypes = [kUTTypeMPEG4 as String] // kUTTypeMPEG4 == "public.mpeg-4"
If you want to only allow files with extension of .mp4
(one of several extensions allowed for MPEG-4 encoding), pass the extension in:
filePicker.allowedFileTypes = ["mp4"]
The examples goes from most generic (all types of movie files), to more specific (only MPEG-4 encoded files) to most restricted (only .mp4
files). Choose one that suits your needs.