我正在尝试创建一个QuickLook插件来播放该包的QuickLook预览中的音频,但我的尝试只显示默认的QL预览-一个较大的文件图标,文件名,类型,大小和修改日期。
对于目标UTI类型,我已经在我的XCode设置中成功地将测试字符串显示为kUTTPlainText,并验证了传递给QLPreviewRequestSetDataRepresentation的CFDataRef不是NULL。
下面是我在GeneratePreviewForURL函数中获得的基本代码:
NSURL *audioFilePath = @"the file path";
CFDataRef data = (__bridge CFDataRef)[NSData dataWithContentsOfURL:audioFilePath];
QLPreviewRequestSetDataRepresentation(preview, data, kUTTypeAudio, NULL);
return noErr;有什么想法吗?从QuickLook预览中播放音频是可能的吗?
发布于 2014-06-09 10:37:25
我是根据我的个人经验得出这个答案的。
在OSX10.6.8上的Xcode4.2中,只需使用<audio>标签的<src>属性就可以在基于HTML语言的QuickLook插件中加载音频文件:
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
@autoreleasepool {
if (QLPreviewRequestIsCancelled(preview)) return noErr;
NSMutableString *html=[[NSMutableString alloc] init];
NSDictionary *props;
props=@{
(__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey:@"UTF-8",
(__bridge NSString *)kQLPreviewPropertyMIMETypeKey:@"text/html",
};
[html appendString:@"<html>"];
[html appendString:@"<body>"];
[html appendString:@"<audio src=\"/tmp/AudioFile.mp3\" type=\"audio/mpeg\" controls=\"true\" autoplay=\"true\" />"];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
QLPreviewRequestSetDataRepresentation(preview,(CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding],kUTTypeHTML,(CFDictionaryRef)props);
}
return noErr;
}现在,在Mavericks上使用Xcode5.1,似乎使用cid:方案(我将在下面发布一个示例)都不能做到这一点:
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
@autoreleasepool {
if (QLPreviewRequestIsCancelled(preview)) return noErr;
NSMutableString *html=[[NSMutableString alloc] init];
NSDictionary *props;
NSData *audioData=[NSData dataWithContentsOfFile:@"/tmp/AudioFile.mp3"];
props=@{
(__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey:@"UTF-8",
(__bridge NSString *)kQLPreviewPropertyMIMETypeKey:@"text/html",
(__bridge NSString *)kQLPreviewPropertyAttachmentsKey:@{
@"AUDIO":@{
(__bridge NSString *)kQLPreviewPropertyMIMETypeKey:@"audio/mpeg",
(__bridge NSString *)kQLPreviewPropertyAttachmentDataKey: audioData,
},
},
};
[html appendString:@"<html>"];
[html appendString:@"<body>"];
[html appendString:@"<audio src=\"cid:AUDIO\" type=\"audio/mpeg\" controls=\"true\" autoplay=\"true\" />"];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
QLPreviewRequestSetDataRepresentation(preview,(CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding],kUTTypeHTML,(CFDictionaryRef)props);
}
return noErr;
}我认为你应该为此使用report a bug to Apple!
发布于 2020-02-18 08:33:58
这里的一些评论提到了在基于HTML的Quicklook插件中使用音频(抱歉,还没有足够的声誉来添加额外的评论)。
我最近在一个需要嵌入音频文件的project I worked on中遇到了同样的问题,但正如其他人所指出的,尝试在Quicklook插件中使用引用为'CID:‘对象的数据失败了。
我找到的解决方案是将数据作为Base64编码嵌入到超文本标记语言中,如下所示:
<audio src="data:audio/wav;base64,{base64Data}">用实际的Base64数据替换{base64Data}。任何NSData对象都可以使用以下命令转换为Base64字符串:
[dataObject base64EncodedStringWithOptions:0]或者,如果您需要在HTML代码中多次引用该数据,则可以创建一个Javascript URL对象并使用该对象:
let soundURL = new URL("data:audio/wav;base64,{base64Data}");
audioElement.src = soundURL;https://stackoverflow.com/questions/20312300
复制相似问题