- (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