1

I have a published (Cocos2d+UIKit)app in the store, which reproduces audio tracks and needs to keep playing them even if the iPhone is locked or the app is in background.

It used to work perfectly well until iOS 5.0 but since iOS 5.1 the app is restarted: sound stops, when user unlocks iPhone doesn't appear active, when opening again it shows the splash screen and initial window.

I tried to debug it but when I lock the iPhone, xCode gets paused in EAGL... I also tried to play the tracks in a MPMoviePlayerViewController and it does the same.

Please help, I am getting many bad reviews every day due to this issue...

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
k20
  • 2,008
  • 1
  • 17
  • 23
  • You should show us some of your code in case you were doing something wrong. – Amit Shah Mar 16 '12 at 10:45
  • 1
    Its quite easy Try this link http://stackoverflow.com/questions/4771105/how-do-i-get-my-avplayer-to-play-while-app-is-in-background Cheers – Tornado Mar 16 '12 at 10:47

2 Answers2

1

It is hard to tell without seeing your code, but as a wild guess, try this line:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Put in your first controller's viewDidLoad method. Let me know if it fixes the problem for you.

------ Update -----

An additional thing to check for 5.1 is to set

[[AVAudioSession sharedInstance] setActive: YES error: nil];

before each AVAudioPlayer play command.

This solved it for my alarm clock app which used to sound at the alarm time perfectly in 4.x and started to get muted in 5.1. I found that the putting the above line before [AVAudioPlayer play] solved the problem for me.

MyCSharpCorner
  • 1,313
  • 11
  • 15
  • Nothing changed, but thanks for your help. I don't know what code should I write as I don't know where the problem comes from – k20 Mar 17 '12 at 17:15
  • Thanks again, I was doing that but it did nothing... :( – k20 Mar 17 '12 at 21:16
1

Think I finally solved it by adding:

 - (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[CCDirector sharedDirector] stopAnimation];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[CCDirector sharedDirector] startAnimation];
      [Appirater appEnteredForeground:YES];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[CCDirector sharedDirector] pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[CCDirector sharedDirector] resume];
}

I am not sure why it was working fine before iOS 5.1, but that fixed it :)

k20
  • 2,008
  • 1
  • 17
  • 23