0
votes

I want my app audio to run in background and I had followed this tutorial but it doesn't worked. My app audio still stopped when I pressed home button and I realised it did not call "applicationDidBecomeActive" or "applicationDidEnterBackground" (problem continued even though I had disabled the setting "Application does not run in background") too. I had been dealing with this for the past week.

So far I had done this steps:

-Added AVFoundation framework and declared

#import <AVFoundation/AVFoundation.h>

enter image description here

-Set up AVAudioSession in audio

NSString *audioName = [NSString stringWithFormat:@"audio%d", (nimages)];
        NSString *soundPath =[[NSBundle mainBundle]pathForResource:audioName ofType:@"mp3"];
        NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
        NSError *error = nil;
        AVAudioPlayer *audio = nil;
        audio = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error];
        audio.numberOfLoops = -1;
        audio.volume = 0.9;
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
            NSLog(@"Unable to load file");                
        }else {
            //Make sure the system follows our playback status
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive: YES error: nil];
            //Load the audio into memory
            [audio prepareToPlay];
        }

-Add rows in plist

enter image description here

Updated:

My app is an audio app which the user can play specific soundtrack of my app even though it enter the background. Is it possible?

3

3 Answers

0
votes

I think it has to be actively playing audio for this to work. If your app isnt' actually an audio application, you might try setting the application up as VoIP instead; this doesn't require an active connection. It also won't block other applications wanting to use audio. Just set the background mode is "App provides Voice over IP services", and then it'll run in the background for up to ten minutes at a time; you could connect a VoIP socket and push content to it if you want to keep it alive or wake up also.

0
votes

I suggest you read "Apple Human Interface" by clicking here

There are limited situations where the App is allowed to work in background, such as (Audio, Downloading, Update..etc), you can find it all at the link I provided you.

0
votes

Use this it will work its a running code.

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/notification.wav", [[NSBundle mainBundle] resourcePath]]];

            NSError *error;

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

            AudioSessionInitialize( NULL, NULL, NULL,NULL);
            UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
            AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
            UInt32 value = YES;
            AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(value), &value);
            AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(value), &value);
            [[AVAudioSession sharedInstance] setActive: YES error: nil];
            AudioSessionSetActive(true);



            // self.appAudioPlayer=audioPlayer;
             AVAudioPlayer  *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
            self.audioPlayerForPlay.delegate = self;
            self.audioPlayerForPlay.numberOfLoops=-1;
            [self.audioPlayerForPlay prepareToPlay];
            UInt32 doChangeDefaultRoute = 1;
            AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);





            self.audioPlayerForPlay = audioPlayer101;
            [audioPlayer101 release];
            [self.audioPlayerForPlay play];