Solution for switching between the receiver and speaker for iOS voice playback
[[UIDevice currentDevice] setProximityMonitoringEnAbled: YES]; // we recommend that you set yes before playing, and NO before playing. This function enables infrared sensing.
// Add a listener
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (sensorStateChange :)
Name: @ "UIDeviceProximityStateDiDChangeNotification"
Object: nil];
// Process listener-triggered events
-(Void) sensorStateChange :( nsicationicationcenter *) notification;
{
// If your phone is near your face and placed near your ears, the audio will be output through the receiver and the screen will be dimmed (power-saving)
If ([[UIDevice currentDevice] proximityState] = YES)
{
NSLog (@ "Device is close to user ");
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlAyAndRecord error: nil];
}
Else
{
NSLog (@ "Device is not close to user ");
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlAyback error: nil];
}
}
// Set the following when initializing the player:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
Sizeof (sessionCategory ),
& SessionCategory );
UInt32 audioRouteOverride = kAudioSessionOverrideAudIoRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
Sizeof (audioRouteOverride ),
& AudioRouteOverride );
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
// Speaker playing by default
[AudioSession setCategory: AVAudioSessionCategoryPlAyback error: nil];
[AudioSession setActive: YES error: nil];
Reference: http://www.cnblogs.com/linyawen/archive/2012/08/24/2654474.html
-------------------- What to look at, this is the split line. I cut you again --------------------------------
Here is a new supplement, because the above practice may have some problems in actual application. I just checked it and did not apply it to the actual project.
In iOS, not all iOS devices have close-range sensors. This section describes how to call the distance sensor of the iPhone.
Use close-range sensors
UIDeviceThere are two close-range sensor attributes: proximityMonitoringEnablEd and proximityState. Both attributes are supported by iOS 3.0 and later.
ProximityMonitoringEnabl Ed attributes
To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityState property remains NO, proximity monitoring is not available.
To determine whether the close sensor is available, you can try to enable it, that is, proximityMonitoringEnabl.Ed = YES. If the set attribute value is still NO, the sensor is unavailable.
ProximityState Property
When the sensor is enabled, if the user approaches the close sensor, the attribute value is YES and the screen is off (non-sleep ). And vice versa.
Notification
UIDeviceProximityStateDiDchangenoication ication occurs when the close sensor status changes.
// Add a close-range event listener. Set it to YES before adding the listener. If there is still NO read after the listener is set, the current device does not have a close-range sensor.
[[UIDevice currentDevice] setProximityMonitoringEnAbled: YES];
If ([UIDevice currentDevice]. proximityMonitoringEnablEd = YES ){
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (sensorStateChange :) name: UIDeviceProximityStateDiDChangeNotification object: nil];
}
// Delete close-range event listening
[[UIDevice currentDevice] setProximityMonitoringEnAbled: YES];
If ([UIDevice currentDevice]. proximityMonitoringEnablEd = YES ){
[[Nsicationcenter center defacenter center] removeObserver: self name: UIDeviceProximityStateDiDChangeNotification object: nil];
}
[[UIDevice currentDevice] setProximityMonitoringEnAbled: NO];
# Pragma mark-handling close monitoring trigger events
-(Void) sensorStateChange :( nsicationicationcenter *) notification;
{
// If your phone is near your face and placed near your ears, the audio will be output through the receiver and the screen will be dimmed (power-saving)
If ([[UIDevice currentDevice] proximityState] = YES) // black screen
{
NSLog (@ "Device is close to user ");
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlAyAndRecord error: nil];
}
Else // black screen
{
NSLog (@ "Device is not close to user ");
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlAyback error: nil];
If (! [MTool isPlayRecodering]) {// you can turn off the distance sensor if no video is played or the screen is black.
[[UIDevice currentDevice] setProximityMonitoringEnAbled: NO];
}
}
}
Note (that is, the question I mentioned) For applications that do not want to start the proximity sensor function, if you need to switch between the speaker and the receiver, you must enable the proximity sensor to switch the sound output mode, you must note that when the sound is played through the receiver, the audio is still output in the receiver mode when the playback is complete. If the sensor function is disabled at this time, when the receiver is left, because the sensor function has been disabled, the application cannot receive the registered sensor change notification again. If the underlying sound output mode fails to be switched, the related sound output will still be output from the receiver, even if the obstacle that causes the sensor to reflect is out of the sensor's scope, the state of the sensor obtained in the application is not close to the State, which invalidates switching the audio output mode based on the sensor state. Special case: In iPhone 4s and iPhone 5, if the sensor status is YES after the proximity sensor function is disabled, no sensor change notification will be received when the sensor is started again; in iPhone 4, if the sensor status is YES after the proximity sensor function is disabled, a sensor change notification is sent when the sensor is started again; Solution to this problem: when the sensor function starts, if the sensor sensing status is YES at this time, the sound playing ends and the sensor status is not changed yet, the sensor function is not disabled at this time. When the obstacle that causes the sensor to reflect is out of the sensor's scope, the sensor will receive a change notification to check whether the current sensor status is enabled and the sound playback status, if the sensor status is YES, and the sensor function must be disabled when the sensor function (such as the sound playback function) is completed;
------- That is to say, close the sensor function when the screen is not black. No problem.
Manually switch to another mode. Code snippet:
UILongPressGestureRecognizer * longPressGestureRecognizer = [[UILongPressGestureRecognizeralloc] initWithTarget: self
Action: @ selector (longPressed :)];
[LongPressGestureRecognizersetMinimumPressDuration: 1.0f];
[LongPressGestureRecognizersetAllowableMovement: 50.0];
[Self. bubbleBgImageViewaddGestureRecognizer: longPressGestureRecognizer];
[LongPressGestureRecognizerrelease];
---------
-(Void) longPressed :( UILongPressGestureRecognizer *) gestureRecognizer
{
Switch (gestureRecognizer. state)
{
CaseUIGestureRecognizerStateEnded:
Break;
CaseUIGestureRecognizerStateCancelled:
Break;
CaseUIGestureRecognizerStateFailed:
Break;
CaseUIGestureRecognizerStateBegan:
If ([self. voiceDelegaterespondsToSelector: @ selector (BaseChartVoiceLongPressed)])
{
[Self. voiceDelegateBaseChartVoiceLongPressed];
}
Break;
CaseUIGestureRecognizerStateChanged:
Break;
Default:
Break;
}
}
-------------
# Pragma mark BaseChartCellDelegate
-(Void) BaseChartVoiceLongPressed
{
NSLog (@ "voice long Pressed ");
If ([[[AVAudioSessionsharedInstance] category] isaudio tostring: AVAudioSessionCategoryPlayback])
{
// Switch to the receiver
[[AVAudioSessionsharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecorderror: nil];
[SelfshowTipInfo: @ "switch to receiver mode"];
}
Else
{
// Switch to speaker playback
[[AVAudioSessionsharedInstance] setCategory: AVAudioSessionCategoryPlaybackerror: nil];
[SelfshowTipInfo: @ "switch to speaker mode"];
}
}