2013-02-10 83 views
1

我已經試過這個,但它不工作,我最近7個小時都在努力,請幫助我。我想將自定義按鈕添加到MPMoviePlayer的全屏視圖中。MPMoviePlayerController在全屏添加子視圖

代碼:

moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

     [self.view addSubview:moviePlayerController.view]; 
     moviePlayerController.fullscreen = YES; 

     UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; 

     CustomControlsViewController *overlay = (CustomControlsViewController*)[mainStoryBoard instantiateViewControllerWithIdentifier:@"Custom Controls"]; 

     [moviePlayerController.view addSubview:overlay.view]; 

     [moviePlayerController play]; 

回答

10

所有的第一次,從來沒有添加任何子視圖MPMoviePlayerController的觀點本身。將它們添加到其背景視圖或其父級作爲兄弟。

此所述MPMoviePlayerController文檔中進行了討論:

考慮一個電影播放視圖是一個不透明結構。您可以將 您自己的自定義子視圖添加到電影頂部的圖層內容,但是您的 絕不能修改其任何現有子視圖。除了在電影上分層內容 之外,您還可以通過向backgroundView屬性中的視圖添加子視圖來提供自定義背景 內容。

第二,使用適當的全屏的情況下,MPMoviePlayerController不會重用其正常視圖,但直接加到它的內容到UIWindow實例。因此,在使用「正確的」全屏模式時,您只有以下選項;找到當前的按鍵窗口,並在切換到全屏模式後直接添加控件。

像這樣的東西應該做的:

//are we in fullscreen-mode? 
if (player.fullscreen) 
{ 
    UIWindow *window = [UIApplication sharedApplication].keyWindow; 
    if (!window) 
    { 
     window = [[UIApplication sharedApplication].windows objectAtIndex:0]; 
    } 
    //we now got a proper window for use of our controls ... 
    //add them to the window instance! 
} 

作爲替代,根本就沒有使用「適當的」全屏,但調整MPMovieViewController的觀點,以覆蓋整個屏幕 - 我稱之爲「假」全屏。此選項的一大優勢是您可以使用/捕獲/覆蓋正常重新定向。

+0

我將在「假」全屏上製作自定義控件,thx! – 1337code 2013-02-11 12:17:19

+0

關於你的第一點 - 我閱讀它的方式,可以向movieplayer視圖添加子視圖,但不要修改任何本機提供的視圖(例如遍歷子視圖並更改它們)。 – Grav 2014-01-29 09:09:27

+0

根據文檔「您可以在此屬性中的視圖中添加子視圖,您可以安全地將子視圖添加到播放器的視圖中。如果要顯示自定義回放控件或添加其他與你的應用程序「。 https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/occ/instp/MPMoviePlayerController/view – SomeGuy 2014-11-17 09:32:52