Is it possible to put audio into 'phone' mode? - iphone

By this I mean, use the speaker and phone microphone but don't use the speaker in the handsfree style.
So the use case is:
1. User is listening to the app via the phones speaker
2. They wish to put the phone to their ear so they choose an option which changes the audio to be as if they were talking on the phone (i.e. non-hands free mode)
Does this make sense? Or should this happen automatically?

You'll want to set the audio session category to AVAudioSessionCategoryPlayAndRecord:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord];
This will route all audio output to the receiver--"the small speaker you hold to your ear when on a phone call". See Audio Session Programming Guide: Configuring the Audio Session for more details.

Related

Objective-c re-route audio input from bluetooth HFP to mix with audio out the headphone jack

I am trying to implement a feature in Objective-C where the use case requires the user to speak into a Hands-Free bluetooth headset and have their voice mix with an audio file and play both over the headphone jack.
I have the program working and will allow mixing of microphone and audio over the HFB, or using a wired microphone. But the audio always plays the same place as the microphone source. I cannot find a way to override only audio output to the headphone jack.
I used the following code, which I found in the documentation to override output to the speaker, but I cannot find one to force audio to the headphone jack:
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
Another problem with the above line of code is that it overrides input and output to the built in speaker and built in microphone.
Back to my use case - I need to use hands free bluetooth to voice over audio going out of the headphone jack. Any ideas how this can be implemented?
I have a very similar use case but after searching the Apple Documentation and speaking with Apple directly, it's not currently possible to set the input and output data sources/ports independently.
Per Apple's documentation:
https://developer.apple.com/library/ios/qa/qa1799/_index.html
If an application uses the setPreferredInput:error: method to select a Bluetooth HFP input, the output will automatically be changed to the Bluetooth HFP output. Moreover, selecting a Bluetooth HFP output using the MPVolumeView's route picker will automatically change the input to the Bluetooth HFP input. Therefore both the input and output will always end up on the Bluetooth HFP device even though only the input or output was set individually.
Submit a bug report to Apple. They may change it if there are enough complaints.

Recording from radio streaming xcode 4

I'm building an iPhone app which play some radio over internet ..
I want to make user able to record audio from my application to the radio channel he wants ..
what framework I must use .. and what code for implementation please ?
all Regards
Unfortunately, there is no way to directly capture from the "audio bus". You can either capture the audio via the internal microphone or headset microphone, but that's it. If you are rendering the audio, you could obviously also write that audio out to a file as well at the same time. That's pretty much your only option.

On iPhone, how do you allow another app to keep running in the background while your app is running?

For example, if I start a song in Pandora and then open Safari to browse the web the Pandora song will keep playing. However, if I start a song in Pandora and then open my app it kills the Pandora song. How do I let Pandora keep playing while my app is running?
You need to tell the iOS system what behavior it should use to integrate the two audio sessions. By default it cancels the background audio when you use audio. But this behavior can be modified as described in the AVAudioSession docs here.
To quote:
Working with Music Players
To play audio from a user’s iPod library along with your own sounds
(as described in iPod Library Access Programming Guide), you must use
a so-called mixable category configuration for your audio session.
There are two, alternative ways to configure an audio session as
mixable:
Use the AVAudioSessionCategoryAmbient (or the equivalent
kAudioSessionCategory_AmbientSound) category—which is always mixable.
Use the mixable category override property
kAudioSessionProperty_OverrideCategoryMixWithOthers, as described in
“Fine-Tuning the Category,” to make an otherwise nonmixable playback
category mixable. Having used one of these options, your sounds will
not interrupt a music player—and neither will a music player’s sounds
interrupt yours.
There are two reasons why your app might "kill" the background app:
It starts to play music of its own
You use too much memory for it to keep playing and the OS closes the background app
There is no special option you need to select; you just need to be a "good citizen."

Using MediaPlayer and AVFoundation Framework simultaniously in XCode

I'm an inexperienced XCode use and programmer in general so keep that in mind. I'm trying to use the microphone on the iPhone while at the same time allowing the user to play audio while using the "iPod music picker", but when the user selects music and plays it the recorder stops working? I have no idea what is going on here? Also, on a side note, how do you implement forward and back iPod buttons?
Thanks so much!
Sounds like this is an Audio Session issue - you're probably in a mode where you can either play or record, but not both at the same time.
Check out the Audio Session Programming Guide which should explain how to configure your session.
AVAudioSessionCategoryPlayAndRecord — Use this category for an application that inputs and outputs audio. The input and output need not occur simultaneously, but can if needed. This is the category to use for audio chat applications.
AVAudioSessionCategoryAmbient - This category allows audio from the iPod, Safari, and other built-in applications to play while your application is playing audio. You could, for example, use this category for an application that provides a virtual musical instrument that a user plays along to iPod audio.

iPhone PlayAndRecord silences all system audio?

In my iPhone app I am trying to record audio and play iPod music at the same time, so I set the audio session category to kAudioSessionCategory_PlayAndRecord. But when I set this, all system audio (including vibrate) doesn't work anymore, although the iPod audio still does work. Does anyone know if this is a bug in the SDK or something, or how to get around it? Please help!
Thanks in advance!
Looking at the documentation for kAudioSessionOverrideAudioRoute tells us that the default for the category PlayAndRecord is to route the audio to the receiver (the speaker used when you're talking on the phone). Is it possible that all the audio is being routed to that and you just can't hear without putting your ear there?
If you want to change where the audio is going you need to call AudioSessionSetProperty, and pass it the constant specifying where you want the audio to go. These constants are
kAudioSessionOverrideAudioRoute_None, which specifies that you wish the audio to be routed to the receiver (as it is now), or
kAudioSessionOverrideAudioRoute_Speaker, which specifies that audio should be routed to the speaker at the bottom of the phone
UInt32 routeVar = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(routeVar), &routeVar);