I have followed as many different examples as I can, most of them in the Apple documentation, but I cannot find out the reason why kAudioSessionProperty_OtherAudioIsPlaying always returns a positive on my devices.
I have initialized and activated the session, yet it still returns positives when my music is paused.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
AVAudioSession* session = [AVAudioSession sharedInstance];
NSError *activationError = nil;
BOOL success = [session setActive: YES error: &activationError];
if (!success) { NSLog(#"%#", activationError); }
UInt32 otherAudioIsPlaying;
UInt32 propertySize = sizeof (otherAudioIsPlaying);
AudioSessionGetProperty (
kAudioSessionProperty_OtherAudioIsPlaying,
&propertySize,
&otherAudioIsPlaying
);
if (otherAudioIsPlaying) {
[session setCategory: AVAudioSessionCategoryAmbient error: nil];
} else {
[session setCategory: AVAudioSessionCategorySoloAmbient error: nil];
}
}
What am I doing wrong?
I had the same problem, managed to fix it by ensuring that an AVAudiosession has been instantiated BEFORE checking the property as follows:
AVAudioSession *session = [AVAudioSession sharedInstance];
Make sure that this occurs before you attempt to check before any other audio is playing and it should work fine :)
Related
My code was working fine on iOS6 but not in iOS 7 , please guide
UInt32 enableMetering = 0;
status = AudioQueueSetProperty(recordState.queue, kAudioQueueProperty_EnableLevelMetering, &enableMetering,sizeof(enableMetering));
if (status) {fprintf(stderr, "Could not enable metering\n"); return NO;}
// this line giving error
**status = AudioQueueStart(recordState.queue, NULL);**
if (status) {fprintf(stderr, "Could not start Audio Queue\n"); return NO;}
recordState.currentPacket = 0;
recordState.recording = YES;
return YES;
ERROR is
Could not start Audio Queue
Error starting recording
please guide ...
#Kalaichelvan's comment is perfect.
I was facing similar problem in iOS 7.1. Add following in AppDelegate's didFinishLaunchingWithOptions :
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive:YES error: nil];
EDIT : Above code is working for me
About 1 in 20 times my app (after launch) does not seems to 'beginReceivingRemoteControlEvents' and hence the ios remote defaults to the native music player. Because of this my app has been rejected (at an update, not initial release), the thing is I can never see it happen when the iPhone is plugged into mac and xcode only when running the app on the phone, below is my audio initialization routine.
Hope someone can help, thanks Mike.
-(void)viewDidAppear:(BOOL)animated
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[[AVAudioSession sharedInstance] setDelegate: self];
//Set the audio category of this app to playback.
NSError *setCategoryError = nil; [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
{
ShowMessageBox(#"Audio error", #"Category failed");
}
//Activate the audio session
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
if (activationError) {
ShowMessageBox(#"Audio error", #"Activate failed");
}
}
// And also
- (BOOL)canBecomeFirstResponder
{
return YES;
}
I want to play a sound even in silent mode in iPhone.
Can it be done by using AVAudioPlayer (Without using AVAudioSession)
(For ios 3.0+)
Thanks in advance.
Actually, you can do this. It is controlled via the Audio Session and has nothing to do with AVAudioPlayer itself. Why don't you want to use AudioSession? They play nice together...
In your app, you should initialize the Audio Session, and then you can also tell indicate what kind of audio you intend to play. If you're a music player, then it sort of makes sense that the user would want to hear the audio even with the ring/silent switch enabled.
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
// Allow playback even if Ring/Silent switch is on mute
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),&sessionCategory);
I have an app that I do this very thing, and use AVAudioPlayer to play audio, and with the ring/silent switch enabled, I can hear the audio.
UPDATE (11/6/2013)
In the app I mentioned above, where I used the code above successfully, I have (for some time) been using the following code instead to achieve the same result:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
BOOL result = NO;
if ([audioSession respondsToSelector:#selector(setActive:withOptions:error:)]) {
result = [audioSession setActive:YES withOptions:0 error:&error]; // iOS6+
} else {
[audioSession setActive:YES withFlags:0 error:&error]; // iOS5 and below
}
if (!result && error) {
// deal with the error
}
error = nil;
result = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
if (!result && error) {
// deal with the error
}
I thought I'd post this as an alternative, in light of the most recent comment to this answer. :-)
MarkGranoff's solution is correct. However, if you prefer to do it in Obj-c instead of C, the following works as well:
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
The above answers are correct. Following is the Swift version.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
//print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
//print("AVAudioSession is Active")
} catch _ as NSError {
//print(error.localizedDescription)
}
} catch _ as NSError {
//print(error.localizedDescription)
}
Swift 4 simple version:
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
This will simply do the trick (using AVAudioSession)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
I am playing music using AVAudioPlayer in background. The problem is: if there is a incoming calling interrupts the player, it will never resume unless switch to foreground and do it manually.
The code is simple, to play it in background:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
url = [[NSURL alloc] initFileURLWithPath:...];
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL];
audio_player.delegate = self;
bool ret = [audio_player play];
delegate to handle interruptions:
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
//tried this, not working [[AVAudioSession sharedInstance] setActive: NO error: nil];
NSLog(#"-- interrupted --");
}
//----------- THIS PART NOT WORKING WHEN RUNNING IN BACKGROUND ----------
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
NSLog(#"resume!");
//--- tried, not working: [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
//--- tried, not working: [[AVAudioSession sharedInstance] setActive: YES error: nil];
//--- tried, not working: [audio_player prepareToPlay];
[audio_player play];
}
Can any one help me?
Found the solution!
I had the same problem, my app was resuming the audio nicely after an interruption, only if my app was open. When it was on the background it failed ro resume playing the audio after an interruption.
I fixed this by adding the following lines of code:
Add this line whenever your app start playing audio.[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
And on the endInterruption method, wait 1 or 2 seconds before resume playing the audio. This to allow the OS to stop using the audio channel.
- (void)endInterruptionWithFlags:(NSUInteger)flags {
// Validate if there are flags available.
if (flags) {
// Validate if the audio session is active and immediately ready to be used.
if (AVAudioSessionInterruptionFlags_ShouldResume) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{
// Resume playing the audio.
});
}
}
}
You can also add this line when your app stops (not pause) playing audio. But is not required.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Try this
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer withFlags:(NSUInteger)flags{
if (flags == AVAudioSessionFlags_ResumePlay) {
[audioPlayer play];
}
Hope it helps.
I am building an iPhone audio app using Audio Sessions. Prototype was functioning till I decided to upgrade to 3.1
After a lot of hunting I finally found that the session activation call was failing with error code 12986.
I havent been able to find the reason for this anywhere.
The NSError object doesnt give any detail. I used the localized* APIs to get more info and this is what I got:
localizedDescription: Operation could not be completed. (OSStatus error -12986.)
localizedFailureReason: <blank>
localizedRecoverySuggestion: <blank>
Anyone know how to find more info about such error codes?
Meanwhile I will continue to dig and update this if my status changes.
My Code for the curious is -
NSError *myErr;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&myErr];
bSuccess= [audioSession setActive: YES error: &myErr];
Dont know what 12986 means exactly but it appears to be tied to the audio capabilities of the device now. And I have a solution!
I noticed that this error was popping up only when I use an iTouch and not on the IPhone. Since I was setting the session category as PlayAndRecord on both I decided to check if that was messing it up on the iTouch. Made the code a little smarter to detect if AudioInputIsAvailable and then set the Category accordingly (PlayBack on ITouch and PlayAndRecord on iPhone). That fixed it!
So it looks like this was being ignored in the prior SDKs. I had not changed anything earlier. :-)
Corrected Code Below:
NSError *myErr;
BOOL bSuccess = FALSE;
BOOL bAudioInputAvailable = FALSE;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
bAudioInputAvailable= [audioSession inputIsAvailable];
if( bAudioInputAvailable)
{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&myErr];
}
else {
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&myErr];
}
bSuccess= [audioSession setActive: YES error: &myErr];
if(!bSuccess)
{
NSLog(#"Unable to Start Audio Session. Terminate Application.");
NSLog([myErr localizedDescription]);
NSLog([myErr localizedFailureReason]);
NSLog([myErr localizedRecoverySuggestion]);
}
I've had similar trouble trying to extract useful information from the error object as well when doing core data operations, i found the following code to be helpful in determining more precisely the cause of an error.
NSError *error;
... your code here ...
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0)
{
for(NSError* detailedError in detailedErrors)
{
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
}
else
{
NSLog(#" %#", [error userInfo]);
}
Sorry i couldn't help you out with your audio problem.
HTH