Use Quick Look inside a Swift cocoa application to preview audio files

淺唱寂寞╮ 提交于 2020-01-02 22:27:13

问题


My application lists audio files (MP3) in a NSTableView, with the object for each row containing a path to the audio file. I would like to be able to preview an audio file with Quick Look (like in Finder) when hitting the space bar while one row is selected.

By looking at related questions and answers, I noticed that the API seems to be private and so it's been very hard to find recent and reliable information or documentation about this, let alone in Swift. What's more, most examples I found related to image preview, and some were actually mentioning the "debug quick look" of Xcode, which is not what I'm interested in at all.

I have seen an FTP client that does Quick Look previews for any file that the Finder can preview, so I'm guessing it is possible to use it inside one's app, especially if the file format is natively supported.

Can someone point me in the right direction?


回答1:


This turned out to be pretty simple actually. All the APIs are public. I do think things got simpler with OS X 10.10, just not super well documented maybe?

Here is an example of a ViewController that has a single button that triggers the standard Quick Look Panel with two items that can be found on the filesystem.

class ViewController: NSViewController {
    @IBAction func showQuickLookPanel(sender: AnyObject) {
        if let panel = QLPreviewPanel.sharedPreviewPanel() {
            panel.dataSource = self
            panel.makeKeyAndOrderFront(self)
        }
    }
}

extension ViewController: QLPreviewPanelDataSource {
    func numberOfPreviewItemsInPreviewPanel(panel: QLPreviewPanel!) -> Int {
        return 2
    }

    func previewPanel(panel: QLPreviewPanel!, previewItemAtIndex index: Int) -> QLPreviewItem! {
        if index == 0 {
            return NSURL(fileURLWithPath: "/Library/Desktop Pictures/Beach.jpg")
        } else {
            return NSURL(fileURLWithPath: "/System/Library/Compositions/Rollercoaster.mov")
        }
    }
}

There are three parts to this.

First, to open the standard Quick Look window just call makeKeyAndOrderFront on the shared panel.

The panel knows what to show because it talks to it's datasource, which is implemented via QLPreviewPanelDataSource. As you can see in my example it simply returns a count of 2 and it can be asked to return an object that implements QLPreviewItem.

For my demo I simply returns NSURL instances to two resources that are included in the system by default. Turns out that NSURL already implements the QLPreviewItem protocol so there is nothing extra to do.

If the items that you want to preview do not easily translate to URLs (files) on the filesystem then you will need to do a more complicated implementation of a QLPreviewItem object.

I bet that pointing to MP3 files will just work fine.



来源:https://stackoverflow.com/questions/32810799/use-quick-look-inside-a-swift-cocoa-application-to-preview-audio-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!