2011-07-27 42 views
3

我正在使用背景音頻播放應用程序。我已經按照應用程序文檔的指示編寫了iPod控件的代碼。我已經實現這個喜歡AVAudio中的remoteControlReceivedWithEvent未被調用

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {  
    if (receivedEvent.type == UIEventTypeRemoteControl) { 

     switch (receivedEvent.subtype) { 

      case UIEventSubtypeRemoteControlTogglePlayPause: 
       [self playButtonPressed:playButton]; 
       [self testing]; 
       break; 

      case UIEventSubtypeRemoteControlPreviousTrack: 
       [self rewButtonReleased:(UIButton *)rewButton]; 
       break; 

      case UIEventSubtypeRemoteControlNextTrack: 
       [self ffwButtonReleased:(UIButton *)ffwButton]; 
       break; 

      default: 
       break; 
     } 
    } 
} 
- (BOOL)canBecomeFirstResponder { 
    NSLog(@"canBecomeFirstResponder"); 
    return YES; 
} 

- (void) viewDidAppear:(BOOL)animated 
{ 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){ 
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
     [self becomeFirstResponder]; 
    } 
} 

- (void) viewWillDisappear:(BOOL)animated 
{ 
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; 
    [self resignFirstResponder]; 
} 

請建議我還有什麼,我需要和我在哪裏犯任何錯誤。問候!

回答

1

您是否將UIBackgroundModes音頻鍵添加到info.plist?請參閱docs

0

除了添加UIBackgroundModes音頻關鍵看你的應用程序的info.plist,我說:

-(void)viewDidLoad 
{ 
    UIDevice* device = [UIDevice currentDevice]; 

    BOOL backgroundSupported = NO; 

    if ([device respondsToSelector:@selector(isMultitaskingSupported)]) 
    { 
     backgroundSupported = device.multitaskingSupported; 
    } 

...你的代碼...

+1

爲什麼?有什麼意義? – openfrog

9

只要確保它能夠成爲第一個響應

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 
+0

不起作用。試用iOS 6+。 – openfrog

5

我花了幾個小時試圖解決同樣的問題,直到我發現這個類似的問題:How can you play music from the iPod app while still receiving remote control events in your app?

我的問題是,我是能夠使混合覆蓋,企圖獲得背景音樂的工作:

UInt32 doSetProperty = 1; 
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, 
        sizeof (doSetProperty), &doSetProperty); 

我刪除這些行(原來他們是沒有必要的背景音樂),並remoteControlReceivedWithEvent開始工作。這是有道理的。如果您指定應用程序應該能夠將其音頻與其他應用程序中正在播放的任何音頻混合(例如,將應用程序的音效與iPod應用程序的音樂混合),那麼其他應用程序應該保持遠程按鈕按下的優先級。

0

在斯威夫特3只是確保:

override var canBecomeFirstResponder: Bool { 

    get { 

     return true 
    } 
}