1

I'm playing a silent music with AVAudioPlayer when user locks the screen, so that my timers won't stop. However, when I play an iPod music with [MPMusicPlayerController applicationMusicPlayer], AVAudioPlayer stops,without receiving any call back. Is there any way so that I can start [MPMusicPlayerController applicationMusicPlayer] playing without stoping AVAudioPlayer playing?

EDIT: Thanks guys, this is the app I'm working on:

It is an Alarm app, this app allows user to lock screen while app is running,and when it is the time of the alarm, app can play iPod music to wake the user.Local notification can not use iPod music as alert sound, so I have to keep the app running while screen is locked.

If user quit the app, it will use local notification as alarm, whose sound is limited to files in bundle.

I can't use UILocalNotification as timer since when in screen locked status(in UIApplicationStatusInactive), app can't receive local notification generated by the system.

CarmeloS
  • 7,868
  • 8
  • 56
  • 103
  • Playing silent audio to keep a time running isn't a good solution. What is the purpose of your timer, there may be a better way to do what you're trying to achieve all together. – Mick MacCallum Dec 01 '12 at 12:31
  • Why are you using silent music to keep your timer continue? You can do alternative by using background and foreground notifications for app. When app is going to enter in background, note current time of timer. When it is going to enter foreground, again record current time and and add total interval passed uptil now and set the timer. – NightFury Dec 01 '12 at 12:55

3 Answers3

1

Apple has architected their backgrounding system to really limit things like this from happening. Essentially, there is no way for the you to keep the application running in the background unless it needs to be there. If you explain what you are trying to accomplish, maybe a better solution can be found but as good practice, never use random backgrounding methods to do other things. I am assuming that you might be using the faint music as a way to show something custom on the main screen, this is not a good idea.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
1

Your app will get rejected if you play a silent audio.

Also as per apple's documentation https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103, notifications cannot have sounds (soundName) which play more than 30 seconds.

So you wont be able to release your app in the store.

Kiran Kumar
  • 1,192
  • 8
  • 10
  • Thanks, I know those policies,and also I know I'm doing hacking, so I'm looking for some better way to realize it. But I'm also looking for Apple's statement saying that "playing background audio may be rejected", so I can report this to my boss, would you mind tell where did you find it? – CarmeloS Dec 02 '12 at 10:59
  • Its not an official statement. There's a famous blog post by a famous company called tapbots : http://tapbots.com/blog/pastebot/pastebot-music-in-background. – Kiran Kumar Dec 02 '12 at 11:01
  • Apple rejected their app. They blogged "With excitement, we submitted the final build to Apple last week. A week passed by and we finally received a notice about Pastebot. Rejected. It wasn’t what we hoped for, but to be honest it was expected. We aren’t allowed to play a silent audio clip in the background. " – Kiran Kumar Dec 02 '12 at 11:02
  • 1
    well, things is a little different here, my app does not run in background, it only plays a silent song while the app is in foreground and then user locks the screen. we used to play audio in background and also got rejected.Playing audio while screen locked(which is in inactive status instead of in background status) seems OK, we already have a version using this hacking on app store. – CarmeloS Dec 02 '12 at 11:49
  • In that case , using [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; might help. AVAudioSessionCategoryAmbient supports audio mixing as per http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html – Kiran Kumar Dec 02 '12 at 12:34
0

I figured it out myself. It is not calling [MPMusicPlayerController +applicationMusicPlayer] that stops AVAudioPlayer, but calling [MPMusicPlayerController -setShuffleMode:], I don't know why calling this would stop AVAudioPlayer, but it is where the problem lies in. Thanks everyone, I think I should paste my complete code next time.

CarmeloS
  • 7,868
  • 8
  • 56
  • 103