iOS music playback Headset Listening event

Source: Internet
Author: User

Unplug headset listening operation

Add a notification, unplug the headset and pause playback
        [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (routechange:) Name: Avaudiosessionroutechangenotification Object:nil];

Notification Method:

/**
 *  Execute this method once the output changes
 *
 *  @param notification output Change notification object
 /
-(void) Routechange: ( Nsnotification *) notification{
    nsdictionary *dic=notification.userinfo;
    int changereason= [Dic[avaudiosessionroutechangereasonkey] intvalue];
    Equals avaudiosessionroutechangereasonolddeviceunavailable indicates that the old output is not available
    if (changereason== avaudiosessionroutechangereasonolddeviceunavailable) {
        avaudiosessionroutedescription *routeDescription=dic [Avaudiosessionroutechangepreviousroutekey];
        Avaudiosessionportdescription *portdescription= [routedescription.outputs firstobject];
        If the original device is a headset
        , suspend if ([Portdescription.porttype isequaltostring:@ "headphones"]) {
            [self.player pause];
        }
    }
}

Remember to cancel the listening:

-(void) dealloc{
    [[Nsnotificationcenter Defaultcenter] removeobserver:self name: Avaudiosessionroutechangenotification Object:nil];
}

Receive Remote Events
Add in the Didfinishlaunchingwithoptions method in Appdelegate:

[[UIApplication sharedapplication] beginreceivingremotecontrolevents];

To receive remote control events

Then in Viewcontroller, implement the proxy:

-(void) Remotecontrolreceivedwithevent: (Uievent *) event {if (Event.type==uieventtyperemotecontrol) {switch (EV
                Ent.subtype) {case Uieventsubtyperemotecontrolplay: [Self.audioplayer play];
            Break
                Case Uieventsubtyperemotecontrolpause: [Self.audioplayer pause];
            Break
                Case UIEVENTSUBTYPEREMOTECONTROLSTOP: [Self.audioplayer stop];
            Break Case Uieventsubtyperemotecontroltoggleplaypause:if (self.audioPlayer.isPlaying) {[SE
                Lf.audioplayer pause];
                }else{[Self.audioplayer play];
            } break;
                Case Uieventsubtyperemotecontrolnexttrack:nslog (@ "Next ...");
            Break
                Case Uieventsubtyperemotecontrolprevioustrack:nslog (@ "Previous ...");
            Break Case UIEventsubtyperemotecontrolbeginseekingforward:nslog (@ "Begin seek forward ...");
            Break
                Case Uieventsubtyperemotecontrolendseekingforward:nslog (@ "End seek forward ...");
            Break
                Case Uieventsubtyperemotecontrolbeginseekingbackward:nslog (@ "Begin to seek backward ...");
            Break
                Case Uieventsubtyperemotecontrolendseekingbackward:nslog (@ "End seek backward ...");
            Break
        Default:break; }
    }
}

Avaudiosession it is necessary to carry out a detailed description:

Each application in iOS has an audio session, which is represented by Avaudiosession.
Avaudiosession also exists in the Avfoundation framework, which is a single case pattern design, accessed through sharedinstance.
When you use Apple devices, you will find that some applications will terminate when you turn on other audio playback, while others can play with other applications, and how to control playback in a variety of audio environments is done through audio sessions.
The following are several session modes for an audio session:

Similarly, if an application is already playing the audio, after opening our application, set the session type to play in the background, at which point the other application audio will stop and play our audio. You can call SetActive:: Method to close a session if you want our program to continue playing audio after it has been played (after closing or exiting to the background);

Avaudiosession *audiosession=[avaudiosession Sharedinstance];
[Audiosession setactive:no withoptions:avaudiosessionsetactiveoptionnotifyothersondeactivation Error:nil];

Note that when the setactive is no, it must be equipped with parameters, otherwise there is no egg to use.
After the playback, setactive for No is a more responsible approach

Original address: Http://www.jianshu.com/p/d486e4f205e4

Related Article

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.