AVPlayer不迅速支持外部webVtt文件字幕

时间:2018-11-22 11:10:55

标签: ios swift avplayer

  

我正在使用swift 3并尝试使用webVtt文件添加字幕,但它无法正常工作并使我的应用程序崩溃。请参阅下面的代码。

 let asset1 = AVAsset(url: URL(string: playUrl)! // HLS video link

   //Create AVMutableComposition
    let videoPlusSubtitles = AVMutableComposition()

    //Adds video track
    let videoTrack = videoPlusSubtitles.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

    try? videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset1.duration),
                                     of: asset1.tracks(withMediaType: .video)[0],
                                     at: kCMTimeZero)

    //Adds subtitle track
    let subtitleAsset = AVAsset(url: URL(string: SubtittleUrl)!)
    print("Subtitle CC: \(SubtittleUrl)")

    let subtitleTrack = videoPlusSubtitles.addMutableTrack(withMediaType: .text, preferredTrackID: kCMPersistentTrackID_Invalid)

    try? subtitleTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset1.duration),
                                        of: subtitleAsset.tracks(withMediaType: .text)[0],
                                        at: kCMTimeZero)
    playerItem = AVPlayerItem(asset: videoPlusSubtitles)
    player = AVPlayer(playerItem: playerItem)
    playerLayer = AVPlayerLayer(player: player)
    playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
    playerLayer?.contentsScale = UIScreen.main.scale
    layer.insertSublayer(playerLayer!, at: 0)
    player?.play()
  

请为我提出解决方案,即如何使用使用vtt或srt扩展名的外部字幕文件在视频上显示字幕。

0 个答案:

没有答案