Customizing subtitles with AVPlayer

早过忘川 提交于 2019-12-20 23:31:52

问题


I was able to display a subtitle track with AVPlayer on iOS 6, but I am not able to customize it. It just shows the same style (a small font size, in white).

Here's how I select the subtitle:

AVMediaSelectionGroup *subtitle = [asset mediaSelectionGroupForMediaCharacteristic: AVMediaCharacteristicLegible];
[self.videoPlayer.currentItem selectMediaOption:subtitle.options[0] inMediaSelectionGroup: subtitle];

And how I'm trying to customize the subtitle:

AVTextStyleRule *rule = [[AVTextStyleRule alloc] initWithTextMarkupAttributes:@{
                         (id)kCMTextMarkupAttribute_ForegroundColorARGB : @[ @1, @1, @0, @0 ],
                         (id) kCMTextMarkupAttribute_ItalicStyle : @(YES)}];

self.videoPlayer.currentItem.textStyleRules = @[rule];

No matter if I put this snippet before or after selecting the subtitle, the result is the same.

The AVPlayer is created with a local (file) URL (a mp4 file).

Any thoughts on how to do this?


回答1:


I asked this question on Apple Developer Forums and I got an answer from an Apple employee:

The textStyleRules property only applies to WebVTT content. Your local file is probably carrying subtitles in TX3G format.

You're right that the documentation doesn't mention this limitation, so you should file a bug so we can get our documentation updated.

So, I'll open a radar to ask that they update the docs and I'll post its number here if someone wants to dupe it.

EDIT:

I created rdar://14923673 to ask Apple to update the docs about this current limitation. I also created rdar://14923755 to ask them to provide support to subtitles in TX3G format.

Please dupe them if you're affected by this issue.




回答2:


I found workaround to modify text foreground color and background correctly. Just separate styles to multiple AVTextStyleRule.

func initSubtitleStyle() {
    let textStyle:AVTextStyleRule = AVTextStyleRule(textMarkupAttributes: [
        kCMTextMarkupAttribute_CharacterBackgroundColorARGB as String: [0.2,0.3,0.0,0.3]
        ])!


    let textStyle1:AVTextStyleRule = AVTextStyleRule(textMarkupAttributes: [
        kCMTextMarkupAttribute_ForegroundColorARGB as String: [0.2,0.8,0.4,0.0]
        ])!

    let textStyle2:AVTextStyleRule = AVTextStyleRule(textMarkupAttributes: [
        kCMTextMarkupAttribute_BaseFontSizePercentageRelativeToVideoHeight as String: 20,
        kCMTextMarkupAttribute_CharacterEdgeStyle as String: kCMTextMarkupCharacterEdgeStyle_None
        ])!

    player.currentItem?.textStyleRules = [textStyle, textStyle1, textStyle2]
}

please do not ask me why, that solution come from try and error XD



来源:https://stackoverflow.com/questions/18113659/customizing-subtitles-with-avplayer

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