I have been trying to do this for a few days now using AVFoundation as well as trying to use MPMoviePlayerViewController. The closest I can get is allowing one to play at a time. I would like to think that this is possible because of Facetime. However, I know this is a little different because there is no separate video file.
Any ideas would help, and thanks.
I'm not sure where this is documented, but to get AVCaptureVideoPreviewLayer and MPMoviePlayerViewController to play together at the same time you need to set a mixable audio session category first.
Here's one way to do that:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
UInt32 mixable = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(mixable), &mixable);
[session setActive:YES error:nil];
See the Audio Session Programming Guide and Audio Session Cookbook for more info.
Have you tried to play video on one thread and recording video on another? That would allow both of them to run while maintaining their separation.
I am developing an iPhone app that translates voice to text.
The component for the Transcribe is third-party.
Until now everything worked just fine.
Today I tried playing a sound (.wav file) after making the Transcription and noticed that I cannot play anything (by AVAudio or system sound). After debugging I found out that this is happening after initing the third-party component; so I think that the component is not releasing something that belongs to the system audio.
My question is : Is there a way to force the iPhone play the sound that I want?
Have you tried setting AVAudioSession category?
NSError *err = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err];
Maybe other category will work, for example AVAudioSessionCategoryPlayAndRecord
The best way IMO is to save AVAudioSession state (its category, for example) before using your third-party component and restore state afterwards.
How to check whether microphone is muted before recording audio in iPhone?
Actually, you don't have to. You have to "warn" AVAudioSession that you are going to record a bit of Audio. Doing so, even if you are muted (on the IPhone, I Have not tested this with any external device) you will be able to record your audio... here is the piece of code that does that:
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryRecord
error: &error];
it even records with a higher volume.
Best of luck...
i have a problem with AVAudioRecorder and AVAudioPlayer.
when i use Player and Record at the same time (eg. for playing sound while recording) the sound is in the quiet internal Speaker. i searched stackoverflow and all i found was this code:
UInt32 *audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
But this doesn't help me :(
When i copyPaste it, i got errors.
What can i do to record and play the loud Speaker at the bottom?
I don't use anything like SCLister oder something...
Thanks in advance
Max
This is a bit old, but this post helped me and I wanted to update it for anyone else who might need it in the future. The code posted at the top is correct - it will take the quiet audio that's being played through the phone speaker and route it to the loudspeaker at the bottom. There is a minor typo in the code, which is why it's giving errors. Here is the correct snippet which will solve this issue:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
Make sure you also activate the audio session right after setting this, before creating your audio player/recorder:
[[AVAudioSession sharedInstance] setActive:YES error:nil];
Last, if you're going to be playing and recording at the same time you'll probably need to set the category and mixing functions too. Here's the entire snippet which will set the category, enable mixing, route the audio to the main speaker, and activate the session. You'll want to do this only once right after the app launches.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
NSLog(#"Mixing: %x", propertySetError); // This should be 0 or there was an issue somewhere
[[AVAudioSession sharedInstance] setActive:YES error:nil];
Hope that helps someone!
I answered it here already: How to get AVAudioPlayer output to the speaker
In short, use this before recording:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
...and use this before playback (on loud speakers or headphones if they are plugged in)
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Only thing I have found about this topic is this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
which must be set when you record your audio if you want to play back at the same time. Give that a try and lemme know.
P.S. Make sure you add the AudioToolbox and AVFoundation frameworks to your project and include them in your .m files.
If you're currently playing through the quiet speakers, and want to play through the loud speaker at the bottom of the iPhone, use this code:
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute
);
This is old question, but none of the other answers helped me... However, I found a solution which I am posting for future reference in case someone needs it.
The solution is described in the following blog post: iOS: Force audio output to speakers while headphones are plugged in .
You need to create new Objective-C class AudioRouter in your project. Then import AudioRouter.h into your header file of the class where you are initiating audio functionality. Now in the corrseponding .m file add the following lines within viewDidLoad method:
AudioRouter *foobar = [[AudioRouter alloc] init];
[foobar initAudioSessionRouting];
[foobar forceOutputToBuiltInSpeakers];
Now you have audio (e.g. AVAudioPlayer) output forced to loudspeaker!
AudioSessionSetProperty is deprecated since iOS7 but the following worked for me.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
I use AVAudioPlayer to play sounds in my app, but it turns off the iPod when a sound plays.
Is there a way to prevent this? I don't want to use System Sounds because I can't control their volume.
Thanks for your help.
This should work for you (taken from my Ambiance app)
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
From the docs:
kAudioSessionProperty_OverrideCategoryMixWithOthers
Changes the mixing behavior of the
kAudioSessionCategory_MediaPlayback
and
kAudioSessionCategory_PlayAndRecord
audio session categories. Setting this
property to TRUE (any nonzero value)
allows mixing of iPod audio with
application audio. Other aspects of
these categories, such as their
Ring/Silent switch behavior, are not
affected.
This property has value of FALSE (0)
by default. When the audio session
category changes, such as during an
interruption, the value of this
property reverts to FALSE. To regain
mixing behavior you must then re-set
this property.
Always check to see if setting this
property succeeds or fails, and react
appropriately; behavior may change in
future releases of iPhone OS.
Available in iPhone OS 3.0 and later.
Declared in AudioServices.h.