Background playback issues for IOS Avplayer Avaudioplayer Audio

Source: Internet
Author: User

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?
    1. Avaudiosession *session = [Avaudiosession sharedinstance];
    2. [Session Setcategory:avaudiosessioncategoryplayback Error:nil];
    3. [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?
    1. [UIApplication sharedapplication] beginreceivingremotecontrolevents];
Otherwise, you can't switch to the next song, and then

[Plain]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. Uibackgroundtaskidentifier newtaskid = uibackgroundtaskinvalid;
    2. [Avplayer play];
    3. Newtaskid = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:null];
    4. if (newtaskid! = Uibackgroundtaskinvalid && Oldtaskid! = uibackgroundtaskinvalid) {
    5. [[UIApplication sharedapplication] endbackgroundtask:oldtaskid];}
    6. 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?
    1. -(void) Viewwillappear: (BOOL) animated
    2. {
    3. [Super viewwillappear:animated];
    4. [UIApplication sharedapplication] beginreceivingremotecontrolevents];
    5. [Self becomefirstresponder];
    6. }
    7. -(void) Viewwilldisappear: (BOOL) animated
    8. {
    9. [Super viewwilldisappear:animated];
    10. [UIApplication sharedapplication] endreceivingremotecontrolevents];
    11. [Self resignfirstresponder];
    12. }

Defined

[Plain]View Plaincopyprint?
    1. -(BOOL) Canbecomefirstresponder
    2. {
    3. return YES;
    4. }

Finally define the remotecontrolreceivedwithevent to deal with specific events such as play, pause, forward, rewind, etc.

[Plain]View Plaincopyprint?
  1. -(void) Remotecontrolreceivedwithevent: (uievent *) receivedevent {
  2. if (Receivedevent.type = = Uieventtyperemotecontrol) {
  3. Switch (receivedevent.subtype) {
  4. Case Uieventsubtyperemotecontroltoggleplaypause:
  5. [Self Playbuttonpressed:playbutton];
  6. [Self testing];
  7. Break
  8. Case Uieventsubtyperemotecontrolprevioustrack:
  9. [Self rewbuttonreleased: (UIButton *) Rewbutton];
  10. Break
  11. Case Uieventsubtyperemotecontrolnexttrack:
  12. [Self ffwbuttonreleased: (UIButton *) Ffwbutton];
  13. Break
  14. Default
  15. Break
  16. }
  17. }
  18. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.