ios 应用程序尝试以模态方式显示父视图控制器崩溃的视图控制器

ilmyapht  于 2023-01-27  发布在  iOS
关注(0)|答案(1)|浏览(130)

线程1:“应用程序尝试以模态方式呈现视图控制器〈AVPlayerViewController:0x15f83ee00〉具有父视图控制器〈ValorantAgentsApp.功能详细信息视图控制器:0x15e918670〉."

尝试在应用程序中播放视频时出现错误。当进入要播放视频的屏幕时,应用程序崩溃。当我按下按钮播放视频时,没有问题。但当我想在屏幕加载时播放时,出现崩溃。

import UIKit
import AVKit
import AVFoundation
import MediaPlayer
import MobileCoreServices

class AbilitiesDetailsViewController: UIViewController, AVPlayerViewControllerDelegate {

    @IBOutlet var skillDetailsLabel: UILabel!
    @IBOutlet var skillNameLabel: UILabel!
    @IBOutlet var heroIconImageView: UIImageView!
    var agentAbilitiesVideoURL = ""
    var choosenAgentSkillName = ""
    var choosenAbilitiesImages : UIImage?
 
    override func viewDidLoad() {
       
        skillNameLabel.text = choosenAgentSkillName
        heroIconImageView.image = choosenAbilitiesImages
        super.viewDidLoad()
  
playAbilitiesVideos()
        
    }
  
    
    func playAbilitiesVideos() {
        let player = AVPlayer(url: URL(string: agentAbilitiesVideoURL)!)
                let controller = AVPlayerViewController()
                                       present(controller, animated: true) {  }
                controller.player = player
                                       addChild(controller)
                view.addSubview(controller.view)
        controller.view.frame = CGRect(x: 50 , y: 50, width: 300, height: 300)
       
                controller.player = player
                controller.showsPlaybackControls = true
                player.isClosedCaptionDisplayEnabled = false
                player.play()
    }

   
    /*
    @IBAction func playVideoUsingURL(_ sender: Any) {
            playGlobalVideo()
        }

        func playGlobalVideo() {
            guard let videoURL = URL(string: agentAbilitiesVideoURL) else {
                return
            }
            let player = AVPlayer(url: videoURL)
            let vc = AVPlayerViewController()
            vc.player = player
            present(vc, animated: true) {
                player.play()
            }
        }
 
    */
}

这是我的第三个屏幕和视图控制器。

4c8rllxm

4c8rllxm1#

执行以下操作之一:view.addSubview(controller.view)present(controller, animated: true) { },但不能同时执行

相关问题