How to Record Audio in iPhone SDK [duplicate] - iphone

I want to record audio in my iPhone app and once the audio is recorded I want a dialog box to open which asks for the filename.
And then I want to save the audio with that filename into my app. How can I do so?

try using AVAudioPlayer and AVAudioRecorder classes for audio recording and playback.Initially you have to establish an audio session using AVAudio session class
AVAudioSession *audioS =[AVAudioSession sharedInstance];
[audioS setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioS setActive:YES error:&error];
NSURL *url = [NSURL fileURLWithPath:#"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
and then initialize the recorder and player classes...Hope this helps

Look at Apple's SpeakHere sample app, available on their iOS developer site.

you can see this question How do I record audio on iPhone with AVAudioRecorder? , which is pretty similar to your question or watch this sample code from apple http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html

Related

Keep UIWebView playing music while App is in background

I think everything is in the title.
I have a UIWebiew that plays music, and when the user use the power or home button, putting the app in background, I want the music to keep playing.
Is it possible ?
Thank you for your help !
You can't actually do this with a UIWebView, but you can still do it. This method still streams the audio off of an online resource via a URL. Here is some code to help you. Now, lets say, you want to stream the audio when the app exits for a video, then its very similar, but uses the MediaPlayer framework instead of AVAudioPlayer framework.
NSURL *url = [NSURL URLWithString:#"http://website.com/audio.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer play];
A little tricky but it is possible. From the official Apple doc below:
https://developer.apple.com/library/ios/qa/qa1668/_index.html
You need to first declare in your plist the UIBackgroundModes for audio, and then write the following code snippet in your AppDelegate
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }
Now when you have audio playing from a UIWebView, you can go to the home screen, change to another app, or lock the screen and the audio will continue playing.
I don't believe this is possible, and even after checking UIWebView's documentation I only found the following three attributes related to media playback.
allowsInlineMediaPlayback property
mediaPlaybackRequiresUserAction property
mediaPlaybackAllowsAirPlay property
Unfortunately, none of them do this.
Yes it is possible. You need to do:
Activate playback session:
#import AVFoundation;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
Add audio background mode in your Info.plist:
Required background modes: App plays audio or streams audio/video using AirPlay
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>

iOS: Displaying currently played track info in LockScreen?

As of iOS 5 we have access to MPNowPlayingInfoCenter to display info in the lockscreen and in the multimedia controls on the multitasking bar. I have an app that plays local audio files. I want to display info like the artist's name, the album and the artwork on the lockscreen with MPNowPlayingInfoCenter, but the only way to do this (As far as I know) is to use MPMusicPlayerController and get nowPlayingItem... The problem is that MPMusicPlayerController is used to play iPod Music only, and not locally stored files. Is there a way around this in iOS 5?
You can create your own NSDictionary and supply that to the MPNowPlayingInfoCenter.
NSArray *keys = [NSArray arrayWithObjects:MPMediaItemPropertyAlbumTitle, MPMediaItemPropertyArtist, ..., nil];
NSArray *values = [NSArray arrayWithObjects:#"Album", #"Artist", ..., nil];
NSDictionary *mediaInfo = [NSDictionary dictionaryWithObjects:values forKeys:keys];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];

OpenFeint Notifications from the bottom

I have both game center and open feint integrated into my app. When the notification comes up to tell you that you successfully logged in, the open feint overlaps the game center notification. How do you move the notification to the bottom instead. I saw the documentation but I can't for the life of me figure out how to get it to work. I see the enum ENotificationPosition and it includes ENotificationPosition_BOTTOM but where am I supposed to put that code to make it work.
All you need to do is specify this in the settings dictionary that you use to initialize OpenFeint. For example,
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:UIInterfaceOrientationPortrait], OpenFeintSettingDashboardOrientation,
kShortAppName, OpenFeintSettingShortDisplayName,
[NSNumber numberWithBool:NO], OpenFeintSettingAlwaysAskForApprovalInDebug,
[NSNumber numberWithBool:YES], OpenFeintSettingGameCenterEnabled,
window, OpenFeintSettingPresentationWindow,
[NSNumber numberWithInt:ENotificationPosition.ENotificationPosition_BOTTOM_LEFT], OpenFeintSettingNotificationPosition,
nil];
[OpenFeint initializeWithProductKey:kOFProductKey
andSecret:kOFProductSecret
andDisplayName:kLongAppName
andSettings:settings
andDelegates:delegates];

Volume too low on iPhone when playing back the recorded file

I am developing an iOS app wherein I should be able to record a sound, save it to the filesystem and then play it back from the disk. When I try to play the file off the disk, I could barely hear anything. The volume is too low. But I'm pretty sure I'd set the device volume to max. I am using AVAudioRecorder and AVAudioPlayer` for recording and playing.
Could someone please point out what the issue could be?
For most the solution here will probably be to use the .defaultToSpeaker option with the AVAudioSessionCategoryPlayAndRecord category:
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker])
But for me the issue turned out to be that a different component in my app was setting the AVAudioSessionModeMeasurement mode on the audio session, which for whatever reason reduces the output volume.
Set category of AVAudioSession using this method
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
try this code. I think setMode fixes my issue.
NSError * outError = nil;
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setActive:NO error:nil];
[ session overrideOutputAudioPort: AVAudioSessionPortOverrideSpeaker error:&outError];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setMode:AVAudioSessionModeSpokenAudio error:nil];
[session setActive:YES error:nil];
if(outError){
NSLog(#"speak error %#", outError);
}

How to record audio through an iPhone app?

I want to record audio in my iPhone app and once the audio is recorded I want a dialog box to open which asks for the filename.
And then I want to save the audio with that filename into my app. How can I do so?
try using AVAudioPlayer and AVAudioRecorder classes for audio recording and playback.Initially you have to establish an audio session using AVAudio session class
AVAudioSession *audioS =[AVAudioSession sharedInstance];
[audioS setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioS setActive:YES error:&error];
NSURL *url = [NSURL fileURLWithPath:#"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
and then initialize the recorder and player classes...Hope this helps
Look at Apple's SpeakHere sample app, available on their iOS developer site.
you can see this question How do I record audio on iPhone with AVAudioRecorder? , which is pretty similar to your question or watch this sample code from apple http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html