views:

119

answers:

1

Hey friend ,

I have problem with AVAudioPlayer in iphone when ever the my ipod is lock(sleep) the song is stop.What I have do in coding for making music is continueslu start even the ipod is lock(sleep) . please help me

I am totally new in this .

A: 

You can use different categories to do this:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{  
    ...

     // allows you to play when the screen is locked and also when the ringer is set to silent
    [[AVAudioSession sharedInstance] setDelegate: self];
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];

    ...
}

If you want to continue to play the sound on iOS4 when your app gets suspended into the background (for instance, the user hits the home button), then you also need to add this to your info.plist file (this shows it in plain text mode):

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

By the way, if you ever really needed to handle an interruption like your question title said (like a phone call, etc), there is a delegate method for that in the AVAudioPlayer class:

- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player { }

and

- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player { }
iWasRobbed
I try the code to avoid the sleep mode interruption it's working but now I have the problem with interruption when call or sms is coming on iphone the song is stop ok at that time.Where the song is stop i want to start the song automatically when the call is end. please help me here thanks in advance
I already told you in the answer.. There are delegate methods which are called where you can monitor for actual interruptions. Use those and call `[player play];` during end interruption.
iWasRobbed