iOS UiView中的Swift Swift Player如何运行 [英] IOS swift avplayer inside UiView how can I make it work

查看:211
本文介绍了iOS UiView中的Swift Swift Player如何运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是刚开始使用AVPlayer for IOS swift并使其正常工作.但是,我希望视频现在可以在UIView中播放,默认情况下,视频占据了整个页面.我一直在尝试一些事情,但是我的代码在这里没有任何作用.我在该页面上还有其他内容,这就是为什么我希望UIView内的avplayer被称为 newView 的原因,并且当我将代码放在下面时,它给了我一个错误.我一直在关注这个示例 UIView图层中的AVPlayerLayer位置

I am new to using the AVPlayer for IOS swift and got it working correctly. However I would like the video to play inside a UIView right now the video takes up the whole page as default . I have been trying a few things and nothing works here is my code . I have other content on that page that is why I would like the avplayer inside the UIVIEW which is called newView and when I put the code below it gives me an error. I have been following this example AVPlayerLayer Position in UIView Layer

    import UIKit
import AVFoundation
import WebKit
import AVKit

class ExampleTable: UIViewController {
    @IBOutlet weak var newView: UIView!

    let avPlayerViewController = CustomAVPLayerC()
    var playerView: AVPlayer?
    var AVLayer: AVPlayerLayer?


    override func viewDidAppear(_ animated: Bool) {

        let movieURL:NSURL? = NSURL(string: "my url")

        playerView = AVPlayer(url: movieURL! as URL)
        avPlayerViewController.player = playerView
        playerView?.isMuted = true
  avPlayerViewController.view.frame = newView.frame
   newView.addSubview(avPlayerViewController.view)
   addChildViewController(avPlayerViewController)
        self.present(self.avPlayerViewController, animated: true) {
            self.avPlayerViewController.player?.play()

        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }


}

但是,当我执行上面的代码时,会出现此错误

However when I do the code above I get this error

2017-08-16 15:55:31.770 Yisly[24335:595275] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <app.ExampleTable: 0x7ff78201a650>.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010aab9b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x0000000109dec141 objc_exception_throw + 48
    2   UIKit                               0x000000010b910e44 -[UIViewController _presentViewController:withAnimationController:completion:] + 5146
    3   UIKit                               0x000000010c28e165 -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 294
    4   UIKit                               0x000000010c28a0e4 -[_UIViewControllerTransitionContext _runAlongsideCompletions] + 155
    5   UIKit                               0x000000010c289dbc -[_UIViewControllerTransitionContext completeTransition:] + 118
    6   UIKit                               0x000000010b8d3b70 -[UITransitionView notifyDidCompleteTransition:] + 251
    7   UIKit                               0x000000010b8d37e8 -[UITransitionView _didCompleteTransition:] + 1408
    8   UIKit                               0x000000010b8d5eb8 -[UITransitionView _transitionDidStop:finished:] + 104
    9   UIKit                               0x000000010b7e6f07 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 222
    10  UIKit                               0x000000010b7e7446 -[UIViewAnimationState animationDidStop:finished:] + 136
    11  QuartzCore                          0x000000010b5ca68e _ZN2CA5Layer23run_animation_callbacksEPv + 306
    12  libdispatch.dylib                   0x000000010da4f05c _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x000000010da3040b _dispatch_main_queue_callback_4CF + 411
    14  CoreFoundation                      0x000000010aa7e909 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    15  CoreFoundation                      0x000000010aa44ae4 __CFRunLoopRun + 2164
    16  CoreFoundation                      0x000000010aa44016 CFRunLoopRunSpecific + 406
    17  GraphicsServices                    0x000000010f203a24 GSEventRunModal + 62
    18  UIKit                               0x000000010b75b0d4 UIApplicationMain + 159
    19  Yisly                               0x0000000105c10117 main + 55
    20  libdyld.dylib                       0x000000010da9b65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

推荐答案

之所以收到错误消息,是因为您试图呈现与刚刚添加为子视图控制器的CustomAVPlayerC实例相同的实例.您无需提供VC,请尝试以下代码,这是一种更简洁的方法:

You are getting the error message because you are trying to present the same instance of your CustomAVPlayerC that you just added as a child view controller. You don't need to present the VC, try this code which is a little cleaner approach:

@IBOutlet weak var newView: UIView!
let avPlayerViewController = CustomAVPlayerC()
var playerView: AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()

    guard let movieURL = URL(string: "my url")
        else { return }

    playerView = AVPlayer(url: movieURL)
    avPlayerViewController.player = playerView
    avPlayerViewController.view.frame = newView.bounds
    self.addChildViewController(avPlayerViewController)
    newView.addSubview(avPlayerViewController.view)
    avPlayerViewController.didMove(toParentViewController: self)
}

我建议阅读实现容器视图控制器的苹果文档"获得处理这样设置的容器视图的最佳实践.

I would recommend going over Apple's documentation on "Implementing a Container View Controller" for best practices in dealing with a container view set up like this.

这篇关于iOS UiView中的Swift Swift Player如何运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆