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
In the Project Navigator, select the Main. storyboard file and open the assistant editor. Control-drag from the Play Video button to the ViewController. swift class to add a new @IBAction method called playVideo .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With