Customizing subtitles with AVPlayer

风流意气都作罢 提交于 2019-12-03 07:15:13

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.

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

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