2012-04-12 85 views
1

當我的應用程序進入後臺並返回時,模式視圖關閉。爲什麼會發生這種情況?我試着暫停和關閉。我啓用了ARC,如果它是一個有用的信息。MPMoviePlayerViewController退出後背景

MPMoviePlayerViewController * player = [[MPMoviePlayerViewController alloc] initWithContentURL:targetURL]; 
[player.moviePlayer prepareToPlay]; 
[self presentMoviePlayerViewControllerAnimated:player]; 
+0

我有同樣的問題。你解決了嗎? – zvjerka24 2012-09-21 09:21:30

+0

這裏是解決方案http://stackoverflow.com/questions/8344877/mpmovieplayerviewcontroller-becomes-black-when-enters-background – zvjerka24 2012-09-21 11:18:03

回答

5

在跨越這種情況下,任何人都跌倒(像我一樣之前,我發現我自己的解決方案),我是能夠防止MPMoviePlayerViewController從退出時應用背景通過繼承它,並刪除它作爲UIApplicationDidEnterBackgroundNotification觀察員:

- (id)initWithContentURL:(NSURL *)contentURL { 

    self = [super initWithContentURL:contentURL]; 

    if (self){ 

     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; 

    } 

    return self; 
} 
0

下面是完整的代碼,可以幫助你

// Call this to start initialization and play movie 
-(void)prepareMoviePlayer:(NSURL *)moviePath 
{ 
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:moviePath]; 

    if ([[moviePlayerViewController moviePlayer] respondsToSelector:@selector(loadState)]) 
    { 
     [[moviePlayerViewController moviePlayer] setControlStyle:MPMovieControlStyleNone]; 
     [[moviePlayerViewController moviePlayer] setFullscreen:YES]; 
     [[moviePlayerViewController moviePlayer] prepareToPlay]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(moviePlayerLoadStateDidChange:) 
               name:MPMoviePlayerLoadStateDidChangeNotification 
               object:nil]; 
    } 
} 

- (void) addPlayer 
{ 
    [[[moviePlayerViewController moviePlayer] view] setFrame:self.view.bounds]; 
    [[self view] addSubview:[[moviePlayerViewController moviePlayer] view]]; 
} 

static NSTimeInterval t; 
// Call this on applicationWillResignActive 
-(void) pauseMovieInBackGround 
{ 
    [[moviePlayerViewController moviePlayer] pause]; 
    t = [[moviePlayerViewController moviePlayer] currentPlaybackTime]; 
    [moviePlayerViewController.view removeFromSuperview]; 
} 

// Call this on applicationWillEnterForeground 
-(void) resumeMovieInFrontGround 
{ 
    [self addPlayer]; 

    [[moviePlayerViewController moviePlayer] stop]; 
    [[moviePlayerViewController moviePlayer] setInitialPlaybackTime:t]; 
    [[moviePlayerViewController moviePlayer] play]; 
} 

-(void)moviePlayerLoadStateDidChange:(NSNotification *)notification 
{ 
    if([[moviePlayerViewController moviePlayer] loadState] != MPMovieLoadStateUnknown) 
    { 

     [[NSNotificationCenter defaultCenter] removeObserver:self 
               name:MPMoviePlayerLoadStateDidChangeNotification 
               object:nil]; 

     [self addPlayer]; 
     [[moviePlayerViewController moviePlayer] play]; 
    } 
} 

-(void)videoPlayBackDidFinish:(NSNotification*)notification 
{ 
    [self dismissMoviePlayerViewControllerAnimated]; 
} 
0

我碰到這個問題來了。
我試過ChrisH的回答,但直到我將removeObserver放入viewDidLoad()而不是init()時,它才適用於我。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; 

} 

然後它工作。好好享受。

2

您不需要子類MPMoviePlayerViewController - 只需在創建MPMoviePlayerViewController實例後執行以下步驟。

[[NSNotificationCenter defaultCenter] removeObserver:self.moviePlayerViewController name:UIApplicationDidEnterBackgroundNotification object:nil]; 
相關問題