2017-08-16 44 views
0

我是新的使用AVPlayer的IOS迅速,並得到它正常工作。不過,我希望視頻能夠在UIView中播放,現在視頻默認會佔用整個頁面。我一直在嘗試幾件事,這裏沒有任何工作是我的代碼。我在該頁面上有其他內容,這就是爲什麼我想要UIVIEW中的avplayer被稱爲newView,當我將代碼放在下面時,它給我一個錯誤。我一直在下面這個例子AVPlayerLayer Position in UIView Layerios swift avplayer裏面的UiView我怎麼能使它工作

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() 

    } 


} 

然而,當我做上面的代碼我得到這個錯誤

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 
+2

remove self.present(self.avPlayerViewController,animated:true){...}? – HMHero

回答

1

你所得到的錯誤消息,因爲你想展示你的CustomAVPlayerC的同一個實例您剛添加爲子視圖控制器。你並不需要出示的VC,試試這個代碼是一個小更簡潔的方法:

@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) 
} 

我會建議在"Implementing a Container View Controller"會超過蘋果的文檔在處理建立這樣的容器視圖的最佳實踐。

+0

非常感謝,現在就閱讀 – user1591668

相關問題