1

I am developing the chat application , If someone sends any message to me & my app is in background then i receive that message and i play the tone whenever message received,while tone is playing in mean while application is opened then music is getting stopped . How to make it continue when app comes to foreground also. (Tone must be complete as per duration available init). How to solve this issue. Waiting for the answer.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Romance
  • 1,416
  • 11
  • 21

2 Answers2

2

You have to work on remote control received events with notification..

- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent {

    if (theEvent.type == UIEventTypeRemoteControl)  {
        switch(theEvent.subtype)        {
            case UIEventSubtypeRemoteControlPlay:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
                break;
            case UIEventSubtypeRemoteControlPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
                break;
            case UIEventSubtypeRemoteControlStop:
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
                break;
            default:
                return;
        }
    }
}

you can view complete answer here

Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • Thanks for answering . But i am playing my tone using UILocalnotification. Then i am having this issue. – Romance May 12 '14 at 06:52
1

You will have to use AVAudioSession. You can have the music temporarily pause or lower in volume while alert plays, then it'll resume full force!

First to initialize audio:

AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]
[audioSession setActive:NO error:nil];
Playing the sound (AVAudioPlayer's play/prepareToPlay will activate the audio session for you):

AVAudioPlayer* audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];

Stopping the alert sound:

[audioPlayer stop];
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:nil];

The flag kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation tells the system to notify background audio to resume playing after the sound is played.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • Thanks for answering . But i am playing my tone using UILocalnotification. Then i am having this issue.Please give me answer in this way. – Romance May 12 '14 at 06:53