Resuming application in background after ios call

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

I use the AVQueuePlayer object when as I transmit my music. Now delegate methods have been deprecated since ios6. Therefore, I use Notifications.

The amazing part is that the end of the interruption (the end of the phone call) is notified, the code that plays music is also recorded, but the jus application does not play music until it appears in the foreground (with a different notification)

Here is 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...............");

    ;
}

}

, Ive . , , . gaana, saavn Apple . . , - . . , . . , , .

. .

+4
2

, , , . , . , , .

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

, .

+3

- , ?

  • :
    OSStatus result1 = AudioSessionInitialize(NULL, NULL, interruptionListener, NULL);
  • 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");
        }
    }
    

    , , .

  • "" ( ), , , , , :
    , AVAudioSession context. ( )

0

All Articles