Avaudioplayer handset playback and external audio playback switch

Source: Internet
Author: User

- (Ibaction)Playpause:(UIButton *)Sender
{
The following settings are set when initializing the player
UInt32Sessioncategory=Kaudiosessioncategory_mediaplayback;
Audiosessionsetproperty(Kaudiosessionproperty_audiocategory,
sizeof(Sessioncategory),
&Sessioncategory);

UInt32Audiorouteoverride=Kaudiosessionoverrideaudioroute_speaker;
Audiosessionsetproperty (Kaudiosessionproperty_overrideaudioroute,
sizeof (Audiorouteoverride),
&Audiorouteoverride);

Avaudiosession *Audiosession= [AvaudiosessionSharedinstance];
Speaker Playback By default
[Audiosession setcategory:AvaudiosessioncategoryplaybackError:Nil];
[Audiosession setActive:YES Error:Nil];

Nserror *Playererror;
Myplayer= [[AvaudioplayerAlloc]Initwithcontentsofurl:[NsurlURLWithString:[VoicerecorderbasevcGetpathbyfilename:Recordedfile OfType:@"AAC"]]Error:&Playererror];
Myplayer.Meteringenabled=YES;
Myplayer.Delegate = Self;


If (Myplayer== Nil)
{
NSLog(@"ERror creating player:%@", [Playererror description]);
}

[SelfHandlenotification:YES];
[Myplayer Play];
}

- (void)Audioplayerdidfinishplaying:(Avaudioplayer *)Player successfully:(BOOL)Flag
{
NSLog(@"Play End");
[SelfHandlenotification:NO];
[Myplayer Release];
}

#pragmaMark- Monitor handsetOrSpeaker
- (void)Handlenotification:(BOOL)State
{
[[UideviceCurrentdevice]Setproximitymonitoringenabled:State]; It is recommended that you set yes before playing, the playback end setting no, this function is to turn on the infrared sensor

If(State)Add Listener
[[NsnotificationcenterDefaultcenter]Addobserver:Self
Selector:@selector(Sensorstatechange:)Name:@"Uideviceproximitystatedidchangenotification"
Object:Nil];
ElseRemove Listener
[[NsnotificationcenterDefaultcenter]Removeobserver:SelfName:@"Uideviceproximitystatedidchangenotification" Object:Nil];
}

Handling Listener Trigger Events
-(void)Sensorstatechange:(Nsnotificationcenter *)Notification;
{
If the phone is placed near the ear at this point, the sound will be output through the handset and darken the screen (save power)
If ([[UideviceCurrentdevice]Proximitystate] ==YES)
{
NSLog(@"Device is close to user");
[[AvaudiosessionSharedinstance]Setcategory:avaudiosessioncategoryplayandrecord Error: nil);
}
else
Span class= "pun" >{
nslog (@ "Device is Not close to user ");
[[avaudiosession sharedinstance< Span class= "pun" >] Setcategory: Avaudiosessioncategoryplayback Error:nil }
} /span>

Avaudioplayer handset playback and external audio playback switch

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.