Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record and play sound in iPhone app?

I tried using AVAudioSession and AVAudioPlayer to record and play sounds respectively but the sound volume is very low.

I tried putting volume value of AVAudioPlayer to 1.0 and more but didn't help much.

What could be my other options to record sound which can be loud enough to play back?

like image 640
Parth Bhatt Avatar asked Apr 14 '11 11:04

Parth Bhatt


People also ask

How do I record audio while using another app?

Swipe down from the top of your screen to see the quick settings tiles and tap the screen recorder button. A floating bubble will appear with a record and microphone button. If the latter is crossed out, you're recording internal audio, and if it's not, you get sound straight from your phone's mic.

Can you record audio and play music on iPhone?

Using the Audio Recorder, you can record your voice, an instrument, or any other sound using the microphone on your iPhone, and play it back in GarageBand.


2 Answers

This code should be useful for you:

#import <AudioToolbox/AudioServices.h>

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;                
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,          
                                 sizeof (audioRouteOverride),&audioRouteOverride);  

It will increase volume. The functionality of the code is to convert the ordinary sound to speaker sound on ur iPhone. That's why kAudioSessionOverrideAudioRoute_Speaker is used.

like image 51
CNSivakumar Avatar answered Oct 18 '22 13:10

CNSivakumar


Since iOS7 you can fix this issue directly with AVAudioSession

The overrideOutputAudioPort method does the same than AudioSessionSetProperty

NSError *setOverrideError;
NSError *setCategoryError;

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];

if(setCategoryError){
    NSLog(@"%@", [setCategoryError description]);
}

[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&setOverrideError];


if(setOverrideError){
    NSLog(@"%@", [setOverrideError description]);
}
like image 41
Martin Christmann Avatar answered Oct 18 '22 12:10

Martin Christmann