Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record audio and save permanently in iOS

I have made 2 iPhone apps which can record audio and save it to a file and play it back again.

One of them uses AVAudiorecorder and AVAudioplayer. The second one is Apple's SpeakHere example with Audio Queues.

Both run on Simulater as well as the Device.

BUT when I restart either app the recorded file is not found!!! I've tried all possible suggestions found on stackoverflow but it still doesnt work!

This is what I use to save the file:

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound1.caf"]; 
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
like image 729
es1 Avatar asked Feb 25 '13 02:02

es1


People also ask

How do I record music and save it to my iPhone?

With the Voice Memos app (located in the Utilities folder), you can use iPhone as a portable recording device to record personal notes, classroom lectures, musical ideas, and more. You can fine-tune your recordings with editing tools like trim, replace, and resume.

How long can iPhone record audio continuously?

There's no time limit for recordings (it's dependent on the internal storage capacity of your device), so you can likely record that entire lecture without worry. Scroll down for tips that take you beyond voice memo recording and to all of the truly cool things you can do with a voice memo app.

How do I secretly record audio on my iPhone?

Run the voice recorder app on your iPhone or iPad. Tap the More Menu (three vertical dots), you can then find the option Start Recording at the Scheduled Time. And then touch the Record button at the bottom left to firstly choose Start Time, then press Continue to to choose End Time. Finally press Record button.

Where do iPhone audio files get saved?

Where are audio messages saved? If you have an iOS version earlier than iOS 12, your saved audio files will be in the Voice Memos app.


1 Answers

Ok I finally solved it. The problem was that I was setting up the AVAudioRecorder and file the path in the viewLoad of my ViewController.m overwriting existing files with the same name.

  1. After recording and saving the audio to file and stopping the app, I could find the file in Finder. (/Users/xxxxx/Library/Application Support/iPhone Simulator/6.0/Applications/0F107E80-27E3-4F7C-AB07-9465B575EDAB/Documents/sound1.caf)
  2. When I restarted the application the setup code for the recorder (from viewLoad) would just overwrite my old file called:

    sound1.caf

  3. with a new one. Same name but no content.

  4. The play back would just play an empty new file. --> No Sound obviously.


So here is what I did:

I used NSUserdefaults to save the path of the recorded file name to be retrieved later in my playBack method.


cleaned viewLoad in ViewController.m :

- (void)viewDidLoad
{

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];

     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

     [audioSession setActive:YES error:nil];

     [recorder setDelegate:self];

     [super viewDidLoad];
}

edited record in ViewController.m :

- (IBAction) record
{

    NSError *error;

    // Recording settings
    NSMutableDictionary *settings = [NSMutableDictionary dictionary];

    [settings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [settings setValue: [NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
    [settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    [settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
    [settings setValue:  [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];

    NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath_ = [searchPaths objectAtIndex: 0];

    NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]];

    // File URL
    NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];


    //Save recording path to preferences
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


    [prefs setURL:url forKey:@"Test1"];
    [prefs synchronize];


    // Create recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    [recorder prepareToRecord];

    [recorder record];
}

edited playback in ViewController.m:

-(IBAction)playBack
{

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

[audioSession setActive:YES error:nil];


//Load recording path from preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


temporaryRecFile = [prefs URLForKey:@"Test1"];



player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];



player.delegate = self;


[player setNumberOfLoops:0];
player.volume = 1;


[player prepareToPlay];

[player play];


}

and added a new dateString method to ViewController.m:

- (NSString *) dateString
{
    // return a formatted string for a file name
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"ddMMMYY_hhmmssa";
    return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"];
}

Now it can load the last recorded file via NSUserdefaults loading it with:

    //Load recording path from preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


temporaryRecFile = [prefs URLForKey:@"Test1"];



player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];

in (IBAction)playBack. temporaryRecFile is a NSURL variable in my ViewController class.

declared as following ViewController.h :

@interface SoundRecViewController : UIViewController <AVAudioSessionDelegate,AVAudioRecorderDelegate, AVAudioPlayerDelegate>
{
......
......
    NSURL *temporaryRecFile;

    AVAudioRecorder *recorder;
    AVAudioPlayer *player;

}
......
......
@end
like image 57
es1 Avatar answered Sep 27 '22 18:09

es1