IOS 4 started with the introduction of the multitask, we can achieve the same as the ipod program in the background to play audio. If the audio operation is implemented with the official Apple Avfoundation.framework, like playing with Avaudioplayer,avplayer, to achieve the perfect background audio playback, depending on the function of the app, you may need to implement several key functions.
First of all, to set the Avaudiosession mode before playing the audio, usually only for playing the app can be set to Avaudiosessioncategoryplayback. Please refer to the documentation for the mode meaning and other modes.
[Plain]View Plaincopyprint?
- Avaudiosession *session = [Avaudiosession sharedinstance];
- [Session Setcategory:avaudiosessioncategoryplayback Error:nil];
- [Session Setactive:yes Error:nil];
1. Notifies the OS that the app supports background audio. By default, when the home key is pressed, the currently running program is suspend and the state changes from active to in-active, that is, if the audio is playing, it will stop when you press home. Here you need to let the app at home, go to the background to run instead of being suspend, the solution is to add required background modes This key item in the program's-info.plist, and select the app plays Audio this value entry.
2. Now that the home button is pressed, the program is back in the background, but the sound is still playing. However, if you want to implement the playlist play, loop play, that is, after the end of a song automatically switch to the next, the problem, when the app in the background after the end of a song, it will stop. The reason is that when running in the background, once the sound stops, the program is suspend, so the program is suspend when you switch the file load gap. Once there is a cottage solution is specialized in a player instance continuously put the same no sound fragment, blocking the program is suspend. The method provided here is to use the background taskid to achieve the background switch to play the file function.
That is, the background task ID is declared, and the app is set as a background task through Beginbackgroundtaskwithexpirationhandler to achieve the purpose of continuous background running. We know that under normal circumstances, press home to send the program to the background, you can have 5 or 10 seconds to do some finishing work, specific time [[UIApplication sharedapplication] backgroundtimeremaining] return value. After timeout, the app will be suspend, and now it's time to start a background task with [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:null]. You can extend the background run time-out for a long time, how much time to extend or see the return value, in short, the music should be enough for a period of time. Another problem is that every starting background task must end with Endbackgroundtask. Therefore, start a new background task after each start of the playback and end the previous background task:
First, in the Viewdidload
[Plain]View Plaincopyprint?
- [UIApplication sharedapplication] beginreceivingremotecontrolevents];
Otherwise, you can't switch to the next song, and then
[Plain]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
- Uibackgroundtaskidentifier newtaskid = uibackgroundtaskinvalid;
- [Avplayer play];
- Newtaskid = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:null];
- if (newtaskid! = Uibackgroundtaskinvalid && Oldtaskid! = uibackgroundtaskinvalid) {
- [[UIApplication sharedapplication] endbackgroundtask:oldtaskid];}
- Oldtaskid = Newtaskid;
Of course, there is a more convenient way is to Beginbackgroundtaskwithexpirationhandler in resignactive: And in becomeactive Endbackgroundtask:
3. We know that the ipod playback program in the background, double-click the Home button, there will be a control interface, you can play control of it.
If you want your app to be able to be controlled by double-clicking the home button like an ipod in the background, you'll need to use a remote control event.
First, declare the app to receive remote control events and end the declaration at the place where the viewdidload is initialized.
[Plain]View Plaincopyprint?
- -(void) Viewwillappear: (BOOL) animated
- {
- [Super viewwillappear:animated];
- [UIApplication sharedapplication] beginreceivingremotecontrolevents];
- [Self becomefirstresponder];
- }
- -(void) Viewwilldisappear: (BOOL) animated
- {
- [Super viewwilldisappear:animated];
- [UIApplication sharedapplication] endreceivingremotecontrolevents];
- [Self resignfirstresponder];
- }
Defined
[Plain]View Plaincopyprint?
- -(BOOL) Canbecomefirstresponder
- {
- return YES;
- }
Finally define the remotecontrolreceivedwithevent to deal with specific events such as play, pause, forward, rewind, etc.
[Plain]View Plaincopyprint?
- -(void) Remotecontrolreceivedwithevent: (uievent *) receivedevent {
- if (Receivedevent.type = = Uieventtyperemotecontrol) {
- Switch (receivedevent.subtype) {
- Case Uieventsubtyperemotecontroltoggleplaypause:
- [Self Playbuttonpressed:playbutton];
- [Self testing];
- Break
- Case Uieventsubtyperemotecontrolprevioustrack:
- [Self rewbuttonreleased: (UIButton *) Rewbutton];
- Break
- Case Uieventsubtyperemotecontrolnexttrack:
- [Self ffwbuttonreleased: (UIButton *) Ffwbutton];
- Break
- Default
- Break
- }
- }
- }
4. At this point, you have the Play app is quite perfect, and the last problem, that is, when users use headphones, the problem comes again. The system defaults to when the headset is plugged in, the sound that is playing is not interrupted, it switches directly to the headset playback, and the playback stops when the headset is unplugged. If this behavior meets your requirements, then OK, otherwise you will need to further investigate the problem of headphone detection and voice routing switching.
Background playback issues for IOS Avplayer Avaudioplayer Audio