【发布时间】:2018-09-16 10:57:59
【问题描述】:
我编写了一个 Quick Look 插件,它试图播放这样的音乐:
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
NSURL *fileURL = (__bridge NSURL*)url;
AudioPlayer *player = // load player with fileURL
// Create a semaphore
sema = dispatch_semaphore_create(0);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
// Start playback and signal the semaphore once finished
[player play:^{
dispatch_semaphore_signal(sema);
}];
// Wait here until the player completion block signals the semaphore to stop waiting
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
NSLog(@"%@", @"done!");
return kQLReturnNoError;
}
由于各种原因,我无法将这些音频文件转码为 macOS 知道的格式,否则我可以将一个 MP3 文件交给操作系统,然后让系统的插件为我播放。因此,我使用带有信号量的肮脏 hack 来停止执行以保留我的播放器对象,否则它会在开始播放后立即停止。
问题在于,由于 quicklookd 进程仍在运行,文件将在 Quick Look 面板停止预览后继续播放。
有没有办法像系统插件被关闭时那样停止播放?
【问题讨论】:
-
你的插件能否实现
CancelPreviewGeneration回调,或者轮询QLPreviewRequestIsCancelled?
标签: objective-c xcode macos cocoa quicklook