AVPlayerLayer shows black screen but sound is working

前端 未结 3 1473
离开以前
离开以前 2021-01-21 03:52

Im trying to display a local recorded video in a AVPlayerLayer which works sometimes. I can hear the audio from the recorded video but can\'t see the v

3条回答
  •  野的像风
    2021-01-21 04:39

    Here's how I set a AVPlayerLayer with the video working (I think what you're missing is the videoGravity parameter).

    let bundle = Bundle.main
    let moviePath = bundle.path(forResource: "myVideo", ofType: "mp4")
    let moviePlayer = AVPlayer(url: URL(fileURLWithPath: moviePath!))
    
    playerLayer = AVPlayerLayer(player: moviePlayer)
    playerLayer.frame = movieView.bounds
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
    movieView.layer.addSublayer(playerLayer)
    playerLayer.player?.play()
    

提交回复
热议问题