2011-02-16 45 views
5

我播放視頻的視圖控制器。當用戶點擊硬件HOME鍵和視頻當前正在播放的應用程序崩潰,在模擬器EXC_BAD_ACCESSiPhone:UIApplicationWillResignActiveNotification從來沒有所謂的

我讀,我應該使用applicationWillResignActive消息以阻止播放視頻應該解決的崩潰。所以我正在嘗試向通知中心註冊此通知,但是我的選擇器永遠不會被調用。我究竟做錯了什麼?

下面的代碼是在我的媒體播放器視圖控制器:

- (void) playMedia {  
    NSURL *mediaUrl = [NSURL fileURLWithPath:tmpFilePath isDirectory:FALSE]; 
    player = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaUrl]; 
    player.moviePlayer.controlStyle = MPMovieControlStyleEmbedded; 

    player.view.frame = self.view.frame;  
    [self.view addSubview:player.view]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(applicationWillResignActive:) 
               name:UIApplicationWillResignActiveNotification 
               object:nil]; 

    [player.moviePlayer play]; 
} 

- (void)applicationWillResignActive:(NSNotification *)notification { 
    // never gets called! 
    NSLog(@"resign active"); 
    [player.moviePlayer stop]; 
} 
+0

雖然你正確地將`nil`傳遞給`addObserver`,對於將來的讀者,我會注意到如果你爲`UIApplicationWillResignActiveNotification`提供了'nil`以外的東西,`selector`可能不會打電話。 – Rob 2013-11-02 06:13:32

回答

1

不知道爲什麼一個心不是爲你工作,但即時通訊在音頻播放器使用

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction:) name:UIApplicationDidEnterBackgroundNotification object:nil];

成功/錄音機。

可能嘗試在應用程序委託實施

- (void)applicationWillResignActive:(NSNotification *)notification { }

,看看它是否調用。

+0

你確定你需要註冊這些,至少在應用程序代表?我得到的applicationDidBecomeActive沒有註冊。但是當您按下主頁按鈕時,我沒有收到任何通知。 – Oscar 2011-05-23 23:30:18

7

請注意,如果您在應用的Info.plist中將UIApplicationExitsOnSuspend鍵設置爲true,則當用戶點擊主頁按鈕時,不會調用applicationWillResignActive方法。

相關問題