2014-10-01 67 views
3

我正在開發iOS中的Cocos2D遊戲,並在控制檯中看到很多約束相關的警告。我在SO和其他網站上閱讀過許多與限制相關的帖子,但一直未能解決這個問題。MPMoviePlayerController在Cocos2D中導致約束警告

我有沒有 NIB文件,並沒有在這個項目中使用XCodes用戶界面生成器。 Cocos2D僅使用編程創建的全屏GLView。據我可以告訴所有這些約束來自與UIView本身交互的MPMoviePlayerController。

我使用的MPMoviePlayerController集成驗證碼:https://github.com/cocos2d/cocos2d-iphone-extensions/blob/master/Extensions/CCVideoPlayer/iOS/CCVideoPlayerImpliOS.m

了一些細微的變化,以顯示視頻非全屏,並得到它編譯於iOS8上:

- (void) setupViewAndPlay 
{ 
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];  

    CGSize viewSize = [[CCDirector sharedDirector] viewSizeInPixels]; 

    // We want to show these animations windowed - not full screen 
    // Native resolution of the movie is 480p = 640x360 
    CGRect videoFrame = CGRectMake(0, 0, 640, 360); 
    if (viewSize.height < 768) 
    { 
     // On the iphone play at 1/2 size 
     videoFrame = CGRectMake(0, 0, 320, 180); 
    } 
    videoFrame.origin.x = (viewSize.width - videoFrame.size.width)/2.0f; 
    videoFrame.origin.y = (viewSize.height - videoFrame.size.height)/2.0f; 
    [keyWindow addSubview: [_theMovie view]]; 
    [[_theMovie view] setHidden: NO]; 
    [[_theMovie view] setFrame: videoFrame]; 
    [[_theMovie view] setCenter: keyWindow.center ]; 
    [keyWindow setAutoresizesSubviews:NO]; 
    [self updateOrientationWithOrientation: (UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]]; 

    // Movie playback is asynchronous, so this method returns immediately. 
    [_theMovie play]; 
} 

這裏是我看到的警告:

2014-10-01 11:32:45.360 SpaceBotAlpha[182:4484] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(
    "<NSLayoutConstraint:0x17774f20 H:|-(34)-[MPKnockoutButton:0x177db4e0](LTR) (Names: '|':_UIBackdropContentView:0x18c7e040)>", 
    "<NSLayoutConstraint:0x17774f50 H:[MPKnockoutButton:0x177db4e0]-(34)-[MPDetailSlider:0x18cb1f60](LTR)>", 
    "<NSLayoutConstraint:0x17774f80 H:[MPDetailSlider:0x18cb1f60]-(34)-[UIView:0x18cb6780](LTR)>", 
    "<NSLayoutConstraint:0x17774fb0 UIView:0x18cb6780.right == _UIBackdropView:0x18caec50.right - 34>", 
    "<NSLayoutConstraint:0x18cafac0 H:|-(0)-[_UIBackdropView:0x18caec50] (Names: '|':MPVideoPlaybackOverlayView:0x1778ad80)>", 
    "<NSLayoutConstraint:0x18cc77b0 H:[_UIBackdropView:0x18caec50]-(0)-| (Names: '|':MPVideoPlaybackOverlayView:0x1778ad80)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1776da10 h=-&- v=-&- _UIBackdropContentView:0x18c7e040.midX == _UIBackdropView:0x18caec50.midX>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1776da40 h=-&- v=-&- _UIBackdropContentView:0x18c7e040.width == _UIBackdropView:0x18caec50.width>", 
    "<NSAutoresizingMaskLayoutConstraint:0x177aa050 h=-&- v=-&- MPVideoPlaybackOverlayView:0x1778ad80.width == MPVideoContainerView:0x1778b8e0.width>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1775e480 h=-&- v=-&- MPVideoContainerView:0x1778b8e0.width == MPSwipableView:0x18cba6f0.width>", 
    "<NSAutoresizingMaskLayoutConstraint:0x177a8ce0 h=-&- v=-&- MPSwipableView:0x18cba6f0.width == MPMovieView:0x177c7eb0.width>", 
    "<NSAutoresizingMaskLayoutConstraint:0x17766310 h=--& v=--& H:[MPMovieView:0x177c7eb0(0)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17774f50 H:[MPKnockoutButton:0x177db4e0]-(34)-[MPDetailSlider:0x18cb1f60](LTR)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

我發現有關約束的所有帖子都假設已經使用了界面構建器。

任何線索?謝謝!


萬一有人發現了這個帖子尋找有關旋轉的信息:我發現了一些在上iOS8上的MPMoviePlayerController奇似乎自動旋轉到正確的方向,如果我離開updateOrientationWithOrientation方法,是(請參閱GitHub鏈接),然後以90度出現所需內容。所以在iOS8上,我檢測updateOrientationWithOrientation內的版本,並在iOS8上不做任何事情而退出。

+0

您是否閱讀了該消息並查看了translatesAutoResizingMaskIntoConstraints? – LearnCocos2D 2014-10-01 07:29:32

+0

我嘗試設置爲YES,並且沒有translatesAutoResizingMaskIntoConstraints開關,但它沒有幫助。這個問題仍然在發生,我仍然在控制檯中收到相同的垃圾郵件。 – Sez 2014-10-18 03:56:22

+1

我剛剛遇到了這個問題,據我所知它是iOS 8.x中的一個錯誤。有關的約束在MPMoviePlayer視圖層次結構中下降了。理論上講,你可以深入研究並設置他們的翻譯自動化MakesMaskIntoConstraints = NO,但我個人選擇讓它獨立。 – 2014-10-28 17:37:07

回答

3

OK,我最終被擺脫CCVideoPlayer extension,我試圖破解而直接使用MPMoviePlayerController的解決了這個!當我開始做Mac時,我必須爲此推出新的解決方案。

擺脫約束信息的關鍵是電影視圖的窗口大小必須與其超視圖大小相匹配。爲此,我使用正確的大小和位置在代碼中構造了一個UIView,並使MPMoviePlayerController的視圖成爲其子視圖。

下面是部分代碼:

_videoFrameView = [[UIView alloc] initWithFrame:frameUI]; 
[_videoFrameView setBackgroundColor:[UIColor redColor]]; 
[mainView addSubview:_videoFrameView]; 

_movie = [[MPMoviePlayerController alloc] init]; 
[_movie setRepeatMode:(MPMovieRepeatModeNone)]; 
[_movie setControlStyle:MPMovieControlStyleNone]; 
[_movie setScalingMode:MPMovieScalingModeAspectFit]; 
[_movie setMovieSourceType:(MPMovieSourceTypeStreaming)]; 
CGRect movieFrame; 
movieFrame.size = frameUI.size; 
[[_movie view] setFrame:movieFrame]; // player's frame size must match parent's 

[_videoFrameView addSubview: [_movie view]]; 
[_videoFrameView bringSubviewToFront:[_movie view]]; 

的方法更詳細的膏shown in this gist。我在this other answer中顯示的想法基於我的方法。

8

您是否嘗試使用MPMoviePlayerViewController而不是MPMoviePlayerController? (它適用於我:-)) 然後將translatesAutoresizingMaskIntoConstraints設置爲YES。 就像這樣:

 MPMoviePlayerViewController *moviePlayerViewController = [MPMoviePlayerViewController new]; 
    [moviePlayerViewController.view setTranslatesAutoresizingMaskIntoConstraints:YES]; 

我希望這對您有用..

+0

這很好地爲我清除約束警告,謝謝!爲了從'MPMoviePlayerViewController'訪問'MPMoviePlayerController'上的所有屬性和方法,你只需要訪問'moviePlayer'屬性,即'moviePlayerViewController.movi​​ePlayer.shouldAutoplay = YES;'而不是'_theMovie.shouldAutoplay = YES; ' – Tina 2015-01-29 19:17:13

9

我遇到了相同的約束問題。但是,sez的答案對我來說並不奏效,所以我挖得更深一些。在我的情況下,在設置視頻控制器視圖的幀並將其添加到超級視圖之前,問題叫做[videoConroller prepareToPlay]

我正在創建我的MPMoviePlayerController的實例,並在viewDidLoad中調用prepareToPlay,因爲此時我有了url。但是,我一直在等待,直到viewDidAppear實際調整視頻播放器的大小並將其添加到視圖層次結構中。

在向層次結構中添加視圖後,簡單地將該方法調用到解決了我的問題。此外,我能夠有一個框架,不匹配父視圖的邊界。

+0

非常感謝很多人! – Sanniv 2015-07-31 16:20:59

0

在iOS 9.1.2下面已解決的問題。此外,警告重複印刷,直到我重新啓動應用程序。這些線解決了它。

moviePlayerController = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl]; 
    [moviePlayerController.view setTranslatesAutoresizingMaskIntoConstraints:NO];