2016-06-02 6 views
1

AVAudioPlayerを使用している他のアプリを使用しているときに、バックグラウンドで音声を再生するアプリを開発しています。カメラを開くと、音楽は消えますが、AppDelegateのアプリライフサイクルのメソッドは呼び出されませんので、曲のプレイリスト位置や再生時間を保存できません。カメラが離れた後にオーディオを再生するには?

さらに、カメラが終了すると、私はバックグラウンドミュージックの再生を再開したいと思いますが、もう一度私がこの変更を観察できるようにするコールバックメソッドは見つかりませんでした。

アプリがバックグラウンドモードで実行されているときに、カメラがアクティブになったこととカメラが終了したことを確認する方法を知っていますか?

+0

カメラはどのように開いていますか? – rmaddy

+0

スライドアップコントロールパネルを使用して、私はそれをやっている方法です。 – Aaronium112

+0

これはあなたのアプリをバックグラウンドに移動させます。私はちょうどそれをテストし、適切なアプリケーションデリゲートメソッドが呼び出されます。 – rmaddy

答えて

1

私はそれをどのように解決したのですか?

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(handleAudioSessionInterruption:) 
               name:AVAudioSessionInterruptionNotification 
               object:[AVAudioSession sharedInstance]]; 

インターラプトを処理します。

-(void)handleAudioSessionInterruption:(NSNotification*)notification 
{ 
    //NSLog(@"%@",notification); 

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey]; //1 Interuption Start, 0 Interuption Ends 
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey]; 

    if ([interruptionType integerValue] == AVAudioSessionInterruptionTypeBegan) 
    { 
     NSLog(@"Player %d interupted",playerNumber); 
     // • Audio has stopped, already inactive 
     // • Change state of UI, etc., to reflect non-playing state 
     [self.playPauseButton setTitle:@">" forState:UIControlStateNormal]; 
     self.playingAudio = NO; 

     return; 
    } 

    if ([interruptionType integerValue] == AVAudioSessionInterruptionTypeEnded) 
    { 
     if ([interruptionOption integerValue] == AVAudioSessionInterruptionOptionShouldResume) 
     { 
      NSLog(@"Player %d Resume",playerNumber); 
      [self.playPauseButton setTitle:@"||" forState:UIControlStateNormal]; 
      self.playingAudio = YES; 

      NSError *error = nil; 
      AVAudioSession *aSession = [AVAudioSession sharedInstance]; 
      [aSession setMode:AVAudioSessionModeDefault error:&error]; //& means value at address 
      [aSession setCategory:AVAudioSessionCategoryPlayback error:&error]; 
      //[aSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error]; 
      //[aSession setMode:AVAudioSessionModeSpokenAudio error:&error]; 
      [aSession setActive: YES error: &error]; 
      [self.audioPlayer play]; 
     } 
     // • Make session active 
     // • Update user interface 
     // • AVAudioSessionInterruptionOptionShouldResume option 
    } 
関連する問題