首页 > 解决方案 > 使用 QuickTime Player 打开一个短视频

问题描述

我的本地机器上有一个简短的视频。我正在尝试NSWorkspace在 QuickTime Player 上打开它。

let stringUrl = "~/Desktop/myrec.mov"
let url = URL(string: stringUrl)!

let config = NSWorkspace.OpenConfiguration()
config.activates = true

NSWorkspace.shared.open(
    [url],
    withApplicationAt: URL(string: "~/Applications/QuickTime Player")!,
    configuration: config
)

我得到的错误是:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file cap/cap.swift

这与withApplicationAt错误的 URL 和返回有关nil

我确定withApplicationAtURL是错的,但我不知道如何找到快速播放器应用程序的网址。

我对 Swift 很陌生,并且在阅读文档时遇到了麻烦,尤其是因为它们似乎不是最新的(例如,我的编译器说openFile已弃用并导致我使用open)。

我也不确定这是否是完成我想做的事情的正确/最佳方式(在 quicktime 播放器上打开一个小型本地视频)。

任何建议、提示、建议或答案将不胜感激!

标签: swift5

解决方案


尝试这个。在我的 Mac 上工作:

let stringUrl = "/Users/whoever/Desktop/myrec.mov"
let url = URL(fileURLWithPath: stringUrl)

let config = NSWorkspace.OpenConfiguration()
config.activates = true

NSWorkspace.shared.open(
    [url],
    withApplicationAt: URL(fileURLWithPath: "/System/Applications/QuickTime Player.app"),
    configuration: config
)

请注意,我将 URL 的初始化程序替换为正确的初始化程序。另请注意,我已将 QuickTime Player 路径与 Finder 提供的路径进行了交换(按住选项键时单击鼠标右键)。


推荐阅读