Ios-about the use of avaudiosession--play music backstage

Source: Internet
Author: User

Avaudiosession is a singleton that can be used directly without instantiation. Avaudiosession plays a very important role in all kinds of audio environments. Different audio session classifications are required for different audio scenarioscategories of 1.1AVAudioSession? Avaudiosessioncategoryambient– Mix play, such as rain, car engine, etc. to play with other music? Avaudiosessioncategorysoloambient– Background play, other music will be stopped? Avaudiosessioncategoryplayback– Exclusive Music playback? Avaudiosessioncategoryrecord– Record Audio? Avaudiosessioncategoryplayandrecord– Play and record audio? Avaudiosessioncategoryaudioprocessing– Use the hardware decoder to process audio, which cannot be played or recorded during the use of the audio session:

Category

Input

Output

Mix with ipod

Follow Mute

Avaudiosessioncategoryambient

No

Yes

Yes

Yes

Avaudiosessioncategorysoloambient

No

Yes

No

Yes

Avaudiosessioncategoryplayback

No

Yes

No

No

Avaudiosessioncategoryrecord

Yes

No

No

No

Avaudiosessioncategoryplayandrecord

Yes

Yes

No

No

2. Play Music in the background 2.1. Turn on the required background mode

Select Targets-->capabilities-->backgroundmodes-->on and tick the audio and airplay options, as

2.2. Activate background playback in APPDELEGATE.M's applicationwillresignactive: Method, as shown in the code below
Defining global Variables in APPDELEGATE.M
Uibackgroundtaskidentifier _bgtaskid;
) application{    //Turn on background processing multimedia events    [[UIApplication sharedapplication] beginreceivingremotecontrolevents]    ; Session=[avaudiosession Sharedinstance];    [Session Setactive:yes Error:nil];    Background playback    [session Setcategory:avaudiosessioncategoryplayback Error:nil];    To do this, you can press the home key to enter the background, play a period of time, a few minutes. But can not continue to play network songs, if you need to continue to play network songs, but also need to apply for background task ID, the specific practice is: _bgtaskid=[appdelegate Backgroundplayerid:_bgtaskid];    The _bgtaskid is the background task Uibackgroundtaskidentifier _bgtaskid; The global variable defined in APPDELEGATE.M}
2.3. Implement the Backgroundplayerid method
 + (Uibackgroundtaskidentifier) Backgroundplayerid: (uibackgroundtaskidentifier) backtaskid{//Set up and activate audio session category    Avaudiosession *session=[avaudiosession Sharedinstance];    [Session Setcategory:avaudiosessioncategoryplayback Error:nil];    [Session Setactive:yes Error:nil];    Allow the application to receive remote control [[UIApplication sharedapplication] beginreceivingremotecontrolevents];    Set background task ID Uibackgroundtaskidentifier newtaskid=uibackgroundtaskinvalid;    Newtaskid=[[uiapplication sharedapplication] Beginbackgroundtaskwithexpirationhandler:nil]; if (newtaskid!=uibackgroundtaskinvalid&&backtaskid!=uibackgroundtaskinvalid) {[[UIApplication sharedAppl    Ication] Endbackgroundtask:backtaskid]; } return newtaskid;} 
2.4. handling interrupt events, such as phone, voice, etc. , pauses playback when the music playback is interrupted, and starts playing when the interrupt is complete. The specific approach is:
--Register an event interrupt notification in notification hubs://Handling interrupt event notifications [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector ( Handleinterreption:) name:avaudiosessioninterruptionnotification object:[avaudiosession sharedInstance]];--> Implement method to receive interrupt notification//Handle interrupt Event-(void) Handleinterreption: (nsnotification *) sender{    if (_played)    {      [ Self.playView.player pause];        _played=no;    }    else    {        [self.playView.player play];        _played=yes;}    }

Ios-about the use of avaudiosession--play music backstage

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.