Ios 如何从AVPlayer中提取ClosedCaption/字幕

Ios 如何从AVPlayer中提取ClosedCaption/字幕,ios,avplayer,avplayeritem,closed-captions,Ios,Avplayer,Avplayeritem,Closed Captions,由于AVPlayer呈现的闭路字幕有时会与其他UI组件重叠,因此我希望在单独的视图中呈现cc 我可以通过将closedCaptionDisplayEnabled设置为NO来关闭AVPlayer的cc渲染,但我没有找到提取要渲染的闭合字幕的方法 有人知道有没有办法从AVPlayer/AVPlayerItem中提取CC字符串吗?我可以用AVMediaTypeClosedCaption识别AVAssetTrack,但我不确定在特定时间内如何提取字符串。提取字幕字符串的关键步骤是 创建输出:让capti

由于AVPlayer呈现的闭路字幕有时会与其他UI组件重叠,因此我希望在单独的视图中呈现cc

我可以通过将closedCaptionDisplayEnabled设置为NO来关闭AVPlayer的cc渲染,但我没有找到提取要渲染的闭合字幕的方法

有人知道有没有办法从AVPlayer/AVPlayerItem中提取CC字符串吗?我可以用AVMediaTypeClosedCaption识别AVAssetTrack,但我不确定在特定时间内如何提取字符串。

提取字幕字符串的关键步骤是

  • 创建输出:
    让captionOutput=avplayerItemCleableOutput()
  • 将自己设置为代理:
    captionOutput.setDelegate(self,queue:DispatchQueue.main)
  • 流准备好后,添加输出:
    player.currentItem?.add(captionOutput)
  • 创建委托扩展以获取标题更改:
  • 扩展视图控制器:AVPlayerItemLeableOutputPushDelegate{
    func易读输出(u输出:AvPlayerItem易读输出,
    DidOutputAttributedString字符串:[NSAttributedString],
    nativeSampleBuffers nativeSamples:[任何],
    forItemTime项目时间:CMTime){
    //您的标题字符串将在此处交付!
    }
    }
    
  • 或者,在播放器上加上字幕:
    captionOutput.suppressPlayerRendering=true
  • 我在这里创建了一个示例项目: