xcode 如何在AVPlayer中播放AVVideoComposition?

7jmck4yq  于 2022-11-18  发布在  其他
关注(0)|答案(3)|浏览(120)

大家好,我一直在创建一个iOS视频编辑应用程序。我已经创建了一个AVVideoComposition,其中包括两个layers.now我想在www.example.com中播放这个组成AVPlayer.asAVVideoComposition不是一个资产,所以我如何在AVPlayer中播放这个?这里是我一直在尝试的示例代码。我听说过AVVideoCompositionCoreAnimation,但也没有工作。

let backgroundLayer = CoverView.layer
let videoLayer = VideoView.layer
let parentLayer = CALayer()
    parentLayer.frame = CGRect(x:20.0 , y: 20.0, width: CoverView.frame.width, height: CoverView.frame.height)
    parentLayer.addSublayer(backgroundLayer)
    parentLayer.addSublayer(videoLayer)

    let composition = AVMutableVideoComposition()

    composition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)
agxfikkp

agxfikkp1#

您可以使用AVPlayerItem初始化AVPlayer,AVPlayerItem允许将合成作为输入,因此无需导出即可播放。

let item = AVPlayerItem(asset: composition)
let player = AVPlayer(playerItem: item)
gcxthw6b

gcxthw6b2#

您需要一个导出器方法this.in AVExportSession有一个方法导出了finish.in此方法您将能够为您的视频生成一个url file.like此

func exportDidFinish(_ session: AVAssetExportSession) {

     guard
        session.status == AVAssetExportSession.Status.completed,
        let outputURL = session.outputURL
        else {
          return
      }

        self.avplayer = AVPlayer(url: outputURL)

               playerController.player = self.avplayer
               self.addChild(playerController)
               view.addSubview(playerController.view)
               playerController.view.frame = view.bounds
               playerController.showsPlaybackControls = false
              avplayer.isMuted = false
uyhoqukh

uyhoqukh3#

您可以播放AVMutableComposition,但不能播放AVMutableVideoComposition。但是您可以将AVMutableVideoComposition用作AVPlayerItem的videoComposition属性。

let composition = AVMutableComposition()
    
//add and configure tracks here    

let videoComposition = AVMutableVideoComposition()
videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)

let item = AVPlayerItem(asset: composition)
item.videoComposition = videoComposition
let player = AVPlayer(playerItem: item)

相关问题