How to programmatically sense the iPhone mute switch?
I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.
How do I test the position of mute?
(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)
Thanks!
iphone
add a comment |
I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.
How do I test the position of mute?
(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)
Thanks!
iphone
add a comment |
I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.
How do I test the position of mute?
(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)
Thanks!
iphone
I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.
How do I test the position of mute?
(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)
Thanks!
iphone
iphone
edited Aug 2 '11 at 10:21
Jane Sales
13k34756
13k34756
asked Nov 13 '08 at 17:09
OlieOlie
18.3k1785126
18.3k1785126
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
add a comment |
I answered a similar question here (link). The relevant code:
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
1
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
1
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
5
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
|
show 5 more comments
Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.
I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// go back to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);
// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;
if (_hasMicrophone) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);
}
else
// Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting audio category! %@", setCategoryError);
return muteSwitch;
}
add a comment |
To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.
-(NSString*)audioRoute
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
NSString *result = (NSString*)state;
[result autorelease];
return result;
}
-(Float32)audioVolume
{
Float32 state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
return state;
}
add a comment |
-(BOOL)isDeviceMuted
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) > 0 ? NO : YES);
}
3
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
add a comment |
Olie,
I believe you can find the answer to your question here:
https://devforums.apple.com/message/1135#1135
I'm assuming you have access to the Developer Forums at Apple.com :)
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
add a comment |
I followed the general theory here and got this to work
http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.
add a comment |
Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.
The code in application:didFinishLaunchingWithOptions:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The code in viewWillAppear on cameraController, if you have to use camera or recording in your app
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
The code in viewWillDisappear on cameraController
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
add a comment |
For Swift
Below framework works perfectly in device
https://github.com/akramhussein/Mute
Just install using pod or download from Git
pod 'Mute'
and use like below code
import UIKit
import Mute
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel! {
didSet {
self.label.text = ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Notify every 2 seconds
Mute.shared.checkInterval = 2.0
// Always notify on interval
Mute.shared.alwaysNotify = true
// Update label when notification received
Mute.shared.notify = { m in
self.label.text = m ? "Muted" : "Not Muted"
}
// Stop after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
Mute.shared.isPaused = true
}
// Re-start after 10 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
Mute.shared.isPaused = false
}
}
}
add a comment |
Here two examples how to use AudioSessionInitialize:
http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f287543%2fhow-to-programmatically-sense-the-iphone-mute-switch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
add a comment |
Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
add a comment |
Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
AudioSessionInterruptionListener inInterruptionListener = NULL;
OSStatus error;
if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
{
NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
}
else
{
gAudioSessionInited = YES;
}
}
SInt32 ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
NSLog(@"*** Error *** could not set Session property to ambient.");
}
answered Nov 15 '08 at 19:03
OlieOlie
18.3k1785126
18.3k1785126
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
add a comment |
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?
– Finn Gaida
Nov 14 '13 at 22:01
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
Does not seem to work on iOS 8.
– Andres Canella
Jun 12 '15 at 19:55
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
"Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!
– Olie
Jun 13 '15 at 1:30
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
Could you please help me: stackoverflow.com/questions/31604629/…
– Ganee....
Aug 6 '15 at 8:09
add a comment |
I answered a similar question here (link). The relevant code:
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
1
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
1
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
5
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
|
show 5 more comments
I answered a similar question here (link). The relevant code:
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
1
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
1
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
5
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
|
show 5 more comments
I answered a similar question here (link). The relevant code:
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
I answered a similar question here (link). The relevant code:
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
edited May 23 '17 at 11:46
Community♦
11
11
answered Jan 16 '11 at 16:47
Chris LaddChris Ladd
2,06512222
2,06512222
1
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
1
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
5
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
|
show 5 more comments
1
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
1
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
5
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
1
1
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
This works great.
– Gerald Kaszuba
Mar 14 '11 at 22:47
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
Exactly what I was looking for. Thanks Pirripli!
– James Skidmore
Mar 23 '11 at 4:55
1
1
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.
– yood
May 6 '11 at 22:17
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'
– Chris Ladd
May 20 '11 at 22:22
5
5
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
No longer works in iOS 5. stackoverflow.com/questions/6901363/…
– morgancodes
Nov 5 '11 at 17:07
|
show 5 more comments
Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.
I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// go back to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);
// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;
if (_hasMicrophone) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);
}
else
// Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting audio category! %@", setCategoryError);
return muteSwitch;
}
add a comment |
Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.
I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// go back to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);
// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;
if (_hasMicrophone) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);
}
else
// Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting audio category! %@", setCategoryError);
return muteSwitch;
}
add a comment |
Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.
I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// go back to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);
// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;
if (_hasMicrophone) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);
}
else
// Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting audio category! %@", setCategoryError);
return muteSwitch;
}
Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.
I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// go back to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);
// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;
if (_hasMicrophone) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);
}
else
// Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting audio category! %@", setCategoryError);
return muteSwitch;
}
edited Dec 21 '12 at 9:15
Praveen-K
3,21111730
3,21111730
answered Aug 2 '11 at 10:24
Jane SalesJane Sales
13k34756
13k34756
add a comment |
add a comment |
To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.
-(NSString*)audioRoute
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
NSString *result = (NSString*)state;
[result autorelease];
return result;
}
-(Float32)audioVolume
{
Float32 state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
return state;
}
add a comment |
To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.
-(NSString*)audioRoute
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
NSString *result = (NSString*)state;
[result autorelease];
return result;
}
-(Float32)audioVolume
{
Float32 state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
return state;
}
add a comment |
To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.
-(NSString*)audioRoute
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
NSString *result = (NSString*)state;
[result autorelease];
return result;
}
-(Float32)audioVolume
{
Float32 state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
return state;
}
To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.
-(NSString*)audioRoute
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
NSString *result = (NSString*)state;
[result autorelease];
return result;
}
-(Float32)audioVolume
{
Float32 state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
if( n )
{
// TODO: Throw an exception
NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
}
return state;
}
answered Aug 3 '10 at 15:14
Martin CowieMartin Cowie
2,20652868
2,20652868
add a comment |
add a comment |
-(BOOL)isDeviceMuted
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) > 0 ? NO : YES);
}
3
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
add a comment |
-(BOOL)isDeviceMuted
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) > 0 ? NO : YES);
}
3
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
add a comment |
-(BOOL)isDeviceMuted
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) > 0 ? NO : YES);
}
-(BOOL)isDeviceMuted
{
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) > 0 ? NO : YES);
}
edited Mar 14 '11 at 22:44
Gerald Kaszuba
20.6k1699138
20.6k1699138
answered Aug 5 '10 at 18:56
Haemish GrahamHaemish Graham
5111
5111
3
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
add a comment |
3
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
3
3
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
(CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)
– Daniel
Oct 19 '10 at 12:52
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).
– Yuvaraj.M
Nov 24 '12 at 13:16
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.
– Daniel
Nov 26 '12 at 10:21
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
@simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.
– Yuvaraj.M
Nov 26 '12 at 12:01
add a comment |
Olie,
I believe you can find the answer to your question here:
https://devforums.apple.com/message/1135#1135
I'm assuming you have access to the Developer Forums at Apple.com :)
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
add a comment |
Olie,
I believe you can find the answer to your question here:
https://devforums.apple.com/message/1135#1135
I'm assuming you have access to the Developer Forums at Apple.com :)
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
add a comment |
Olie,
I believe you can find the answer to your question here:
https://devforums.apple.com/message/1135#1135
I'm assuming you have access to the Developer Forums at Apple.com :)
Olie,
I believe you can find the answer to your question here:
https://devforums.apple.com/message/1135#1135
I'm assuming you have access to the Developer Forums at Apple.com :)
edited Nov 13 '08 at 20:33
answered Nov 13 '08 at 20:15
jpmjpm
7,938335866
7,938335866
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
add a comment |
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!
– Olie
Nov 15 '08 at 7:42
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
This comment is not very helpful. Can you at least post a quote from the link you posted?
– der_michael
Nov 20 '18 at 18:16
add a comment |
I followed the general theory here and got this to work
http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.
add a comment |
I followed the general theory here and got this to work
http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.
add a comment |
I followed the general theory here and got this to work
http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.
I followed the general theory here and got this to work
http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.
answered Apr 22 '10 at 5:43
Mark24x7Mark24x7
1,3321109
1,3321109
add a comment |
add a comment |
Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.
The code in application:didFinishLaunchingWithOptions:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The code in viewWillAppear on cameraController, if you have to use camera or recording in your app
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
The code in viewWillDisappear on cameraController
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
add a comment |
Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.
The code in application:didFinishLaunchingWithOptions:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The code in viewWillAppear on cameraController, if you have to use camera or recording in your app
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
The code in viewWillDisappear on cameraController
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
add a comment |
Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.
The code in application:didFinishLaunchingWithOptions:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The code in viewWillAppear on cameraController, if you have to use camera or recording in your app
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
The code in viewWillDisappear on cameraController
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!
Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.
The code in application:didFinishLaunchingWithOptions:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The code in viewWillAppear on cameraController, if you have to use camera or recording in your app
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
The code in viewWillDisappear on cameraController
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!
answered Dec 9 '14 at 18:47
slamorslamor
2,98521012
2,98521012
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
add a comment |
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.
– Ganee....
Jul 30 '15 at 7:31
add a comment |
For Swift
Below framework works perfectly in device
https://github.com/akramhussein/Mute
Just install using pod or download from Git
pod 'Mute'
and use like below code
import UIKit
import Mute
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel! {
didSet {
self.label.text = ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Notify every 2 seconds
Mute.shared.checkInterval = 2.0
// Always notify on interval
Mute.shared.alwaysNotify = true
// Update label when notification received
Mute.shared.notify = { m in
self.label.text = m ? "Muted" : "Not Muted"
}
// Stop after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
Mute.shared.isPaused = true
}
// Re-start after 10 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
Mute.shared.isPaused = false
}
}
}
add a comment |
For Swift
Below framework works perfectly in device
https://github.com/akramhussein/Mute
Just install using pod or download from Git
pod 'Mute'
and use like below code
import UIKit
import Mute
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel! {
didSet {
self.label.text = ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Notify every 2 seconds
Mute.shared.checkInterval = 2.0
// Always notify on interval
Mute.shared.alwaysNotify = true
// Update label when notification received
Mute.shared.notify = { m in
self.label.text = m ? "Muted" : "Not Muted"
}
// Stop after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
Mute.shared.isPaused = true
}
// Re-start after 10 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
Mute.shared.isPaused = false
}
}
}
add a comment |
For Swift
Below framework works perfectly in device
https://github.com/akramhussein/Mute
Just install using pod or download from Git
pod 'Mute'
and use like below code
import UIKit
import Mute
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel! {
didSet {
self.label.text = ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Notify every 2 seconds
Mute.shared.checkInterval = 2.0
// Always notify on interval
Mute.shared.alwaysNotify = true
// Update label when notification received
Mute.shared.notify = { m in
self.label.text = m ? "Muted" : "Not Muted"
}
// Stop after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
Mute.shared.isPaused = true
}
// Re-start after 10 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
Mute.shared.isPaused = false
}
}
}
For Swift
Below framework works perfectly in device
https://github.com/akramhussein/Mute
Just install using pod or download from Git
pod 'Mute'
and use like below code
import UIKit
import Mute
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel! {
didSet {
self.label.text = ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Notify every 2 seconds
Mute.shared.checkInterval = 2.0
// Always notify on interval
Mute.shared.alwaysNotify = true
// Update label when notification received
Mute.shared.notify = { m in
self.label.text = m ? "Muted" : "Not Muted"
}
// Stop after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
Mute.shared.isPaused = true
}
// Re-start after 10 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
Mute.shared.isPaused = false
}
}
}
answered Mar 29 '18 at 9:12
Govind PrajapatiGovind Prajapati
842523
842523
add a comment |
add a comment |
Here two examples how to use AudioSessionInitialize:
http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
add a comment |
Here two examples how to use AudioSessionInitialize:
http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
add a comment |
Here two examples how to use AudioSessionInitialize:
http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/
Here two examples how to use AudioSessionInitialize:
http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/
answered Dec 25 '08 at 19:55
catlancatlan
19.2k85666
19.2k85666
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
add a comment |
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
Link is broken.
– Daniel Springer
Nov 6 '18 at 22:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f287543%2fhow-to-programmatically-sense-the-iphone-mute-switch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown