Resuming an app running in background after a phone call ios

I have an ios app that continues to play music when it moves into background. Now, if there is a phone call, answered or not, the app does not resume to play music. For two days i ve been reading posts about this problem here.None of them solved my problem.

I m using an AVQueuePlayer object as I stream my music too when required. Now the delegate methods have been deprecated since ios6. So I am using Notications.

The amazing part is that the interruption end (phone call end) is notified, the code that plays music is also written but the app jus doesnt play music until it comes to foreground(that with another notification)

Here's my code

 -(void)viewWillAppear
 {.....
  ........
   .....
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:UIApplicationDidBecomeActiveNotification object:nil]
}

-(void)audioInterruptionNotification:(NSNotification *) aNotification {

NSLog(@"Interrupt %@", aNotification);
NSDictionary *dict = [aNotification userInfo];
NSUInteger typeKey = [[dict objectForKey:@"AVAudioSessionInterruptionTypeKey"] unsignedIntegerValue]; NSLog(@"%d", typeKey);
if (typeKey == 0)
{
        [avPlayer play];
        NSLog(@"1.......");
  }
else if (typeKey == 1)
{
    [avPlayer play];
    NSLog(@"3...............");

    ;
}

}

Also , Ive tried inducing delays by dispatch queue. Delegate methods dont seem to work. But gaana, saavn and official music app by apple resume after a call. So this is possible. I jus seem to miss out on something. If I use core telephony.I ll have to add an entire framework which will increase the size of the app. And if it is the only way.Please tell how.

Thanks.I really appreciate your time.


So i returned after a while now and i see my question still unanswered. Well I have solved my question. It turns out it was just one simple statement that was missing.

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

along with the code i ve written in my question.


I have solve the problem of - App is interrupted by a phone call , and music disappears how?

  • When app start, call :
    OSStatus result1 = AudioSessionInitialize(NULL, NULL, interruptionListener, NULL);
  • Achieve interruptionListener :

    void interruptionListener( void * inClientData, UInt32 inInterruptionState){
        if (inInterruptionState == kAudioSessionBeginInterruption)
        {
            NSLog(@"Begin kAudioSessionBeginInterruption");        
            alcMakeContextCurrent(NULL); // important
        }
        else if (inInterruptionState == kAudioSessionEndInterruption)
        {
            AVAudioSession * audioSession = [AVAudioSession sharedInstance];
            NSError * err = nil;
            [audioSession setCategory :AVAudioSessionCategoryPlayback error:&err];      // important
            if(err){
                NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
                return;
            }
            err = nil;
            [audioSession setActive:YES error:&err];
            if(err){
                NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
                return;
            }
    
            //AudioSessionSetActive(true); //sometimes have no effect
    
            // use alcMakeContextCurrent to set the  context——with the context you stored before   // important
            NSLog(@"End kAudioSessionEndInterruption");
        }
    }
    

    now it's ok when clock or phone call interrupts, music can be played still.

  • But if you answer the phone and touch down 'HOME'(have not hung up), and go back to hung up, and go back to app, the music can not be played, you should do this :
    When the app go from background to foreground, set the AVAudioSession and context . (not just set them when interruption end)

  • 链接地址: http://www.djcxy.com/p/57454.html

    上一篇: 当youtube应用恢复视频时,如何阻止我的服务中的音乐?

    下一篇: 打电话后,恢复在后台运行的应用程序