在OS X上使用AVFoundation AVPlayer显示字幕 [英] Show subtitles with an AVFoundation AVPlayer on OS X

查看:480
本文介绍了在OS X上使用AVFoundation AVPlayer显示字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在OS X上使用AVFoundation播放视频时显示字幕。



我查看了文档,找不到办法启用字幕轨道。



在iOS上,方法 - [AVPlayerItem selectMediaOption:inMediaSelectionGroup: ] 用于启用字幕轨道。此方法在10.7 SDK中不可用。是否有其他方式显示字幕?



编辑:
Quicktime Player X有字幕支持,例如打开此电影字幕子菜单出现以提供语言选择,并将在选择英语时显示。这导致我相信,他们被包括在API ...

解决方案

我碰到这个问题自己。我发现,不幸的是,除了切换到QTKit,唯一的方法是做一个单独的字幕图层(一个CATextLayer),并将其适当的位置作为子图层到播放器层。这个想法是,你设置一个周期性的时间观察器来触发每隔一秒左右,并更新字幕,以及(这是可选的)一些UI元素,你可能有,显示了在视频中流逝的时间。



我创建了一个基本的SubRip(.srt)文件解析器类;您可以在这里找到: https://github.com/sstigler/SubRip-for-Mac。请务必检查wiki以获取文档。



您可能遇到的另一个挑战是如何动态调整CATextLayer的高度,以适应不同长度的字幕,以及包含视图的不同宽度(如果您选择使其为用户可调整大小)。我发现了一个伟大的CALayoutManager子类,做到了这一点,并对其进行了一些修订,以使其工作,我的努力: https://github.com/sstigler/height-for-width



我希望这有助于。 p>

I'm trying to display subtitles when playing video using AVFoundation on OS X.

I've looked through the documentation and I can't find a way to enable a subtitle track. The API contains multiple references to subtitle tracks, which leads me to believe that it's supported.

On iOS the method -[AVPlayerItem selectMediaOption:inMediaSelectionGroup:] is used to enable subtitle tracks. This method isn't available in the 10.7 SDK. Is there another way to show subtitles?

EDIT: Quicktime Player X has subtitle support, for example on opening this movie the subtitle submenu appears to offer a choice of language, and will display them when English is chosen. This leads me to believe that they're included in the API...

解决方案

I ran into this same issue myself. I found that unfortunately the only way to do it, other than switching to QTKit, is to make a separate subtitles layer (a CATextLayer) and position it appropriately as a sublayer to the player layer. The idea is that you set up a periodic time observer to trigger every second or so and update the subtitles, along with (and this is optional) some UI element you might have that shows what the elapsed time is in the video.

I created a basic SubRip (.srt) file parser class; you can find it here: https://github.com/sstigler/SubRip-for-Mac . Be sure to check the wiki for documentation. The class is available under the terms of the BSD license.

Another challenge you might run into is how to dynamically adjust the height of the CATextLayer to account for varying lengths of subtitles, and varying widths of the containing view (if you choose to make it user-resizable). I found a great CALayoutManager subclass that does this, and made some revisions to it in order to get it to work for what I was trying to do: https://github.com/sstigler/height-for-width .

I hope this helps.

这篇关于在OS X上使用AVFoundation AVPlayer显示字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆