首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QuickLook生成器音频

QuickLook生成器音频
EN

Stack Overflow用户
提问于 2013-12-01 20:52:45
回答 2查看 516关注 0票数 2

我正在尝试创建一个QuickLook插件来播放该包的QuickLook预览中的音频,但我的尝试只显示默认的QL预览-一个较大的文件图标,文件名,类型,大小和修改日期。

对于目标UTI类型,我已经在我的XCode设置中成功地将测试字符串显示为kUTTPlainText,并验证了传递给QLPreviewRequestSetDataRepresentation的CFDataRef不是NULL。

下面是我在GeneratePreviewForURL函数中获得的基本代码:

代码语言:javascript
复制
NSURL *audioFilePath = @"the file path";
CFDataRef data = (__bridge CFDataRef)[NSData dataWithContentsOfURL:audioFilePath];
QLPreviewRequestSetDataRepresentation(preview, data, kUTTypeAudio, NULL);
return noErr;

有什么想法吗?从QuickLook预览中播放音频是可能的吗?

EN

回答 2

Stack Overflow用户

发布于 2014-06-09 10:37:25

我是根据我的个人经验得出这个答案的。

在OSX10.6.8上的Xcode4.2中,只需使用<audio>标签的<src>属性就可以在基于HTML语言的QuickLook插件中加载音频文件:

代码语言:javascript
复制
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:方案(我将在下面发布一个示例)都不能做到这一点:

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2020-02-18 08:33:58

这里的一些评论提到了在基于HTML的Quicklook插件中使用音频(抱歉,还没有足够的声誉来添加额外的评论)。

我最近在一个需要嵌入音频文件的project I worked on中遇到了同样的问题,但正如其他人所指出的,尝试在Quicklook插件中使用引用为'CID:‘对象的数据失败了。

我找到的解决方案是将数据作为Base64编码嵌入到超文本标记语言中,如下所示:

代码语言:javascript
复制
<audio src="data:audio/wav;base64,{base64Data}">

用实际的Base64数据替换{base64Data}。任何NSData对象都可以使用以下命令转换为Base64字符串:

代码语言:javascript
复制
[dataObject base64EncodedStringWithOptions:0]

或者,如果您需要在HTML代码中多次引用该数据,则可以创建一个Javascript URL对象并使用该对象:

代码语言:javascript
复制
let soundURL = new URL("data:audio/wav;base64,{base64Data}");
audioElement.src = soundURL;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20312300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档