Note that there are some explanatory texts on larger screens.

plurals
  1. POxcode: How to save audio file after recording audio using AVFoundation
    text
    copied!<p>I browsed through all kinds of post related to this topic but the answers just do not really help. I used <a href="http://www.techotopia.com/index.php/Recording_Audio_on_an_iPhone_with_AVAudioRecorder_%28iOS_4%29" rel="nofollow noreferrer">this</a> tutorial to implement recording of audio files and playback. What seems to be missing is how to save the record <strong>permanently</strong>. When I exit my app the sound file is there but nothing is in it. I don't even know if it is saving the rocerd or just creating the file. Here is a code sample:</p> <pre><code>NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docsDir = [dirPaths objectAtIndex:0]; NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"tmpSound.caf"]; tempRecFile = [NSURL fileURLWithPath:soundFilePath]; NSDictionary *recSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, nil]; recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil]; [recorder setDelegate:self]; [recorder prepareToRecord]; [recorder record]; </code></pre> <p>I saw a possible solution to this problem <a href="https://stackoverflow.com/a/6855160/1050722">here</a> but since I'm new to iOS I do not really get it or it is just does not working for me.</p> <p>Please help and provide a code example. I read the documentation about AVFoundation and other classes but with no positive results. </p> <p>SOS :)</p> <p><strong>UPDATE</strong> This is my recording method:</p> <pre><code>-(IBAction)recording{ //***** METHOD for recording if(isNotRecording){ isNotRecording = NO; [recButton setTitle:@"Stop" forState:UIControlStateNormal]; recStateLabel.text = @"Recording"; [recorder setDelegate:self]; [recorder prepareToRecord]; [recorder record]; } else{ isNotRecording = YES; [recButton setTitle:@"Rec Begin" forState:UIControlStateNormal]; playButton.hidden = NO; recStateLabel.text = @"Not recording"; [recorder stop]; } } </code></pre> <p>This is my viewDidLoad:</p> <pre><code>- (void)viewDidLoad { //record sound test code - start isNotRecording = YES; //playButton.hidden = YES; recStateLabel.text = @"Not recording"; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [audioSession setActive:YES error:nil]; //record sound test code - END [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // code for generating the sound file that is to be saved NSArray *dirPaths; NSString *docsDir; dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, nil]; NSError *error = nil; recorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&amp;error]; if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { [recorder prepareToRecord]; } } </code></pre> <p>This is my playBack method:</p> <pre><code>-(IBAction)playback{ //AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; NSError *error; // setting the uri of the formed created file NSArray *dirPaths; NSString *docsDir; dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&amp;error]; [player setNumberOfLoops:0]; [player prepareToPlay]; [player setVolume:1]; [player play]; NSLog(@"URL je: %@", soundFileURL); //method 2 if (error) NSLog(@"Error: %@", [error localizedDescription]); else { [player play]; if(player.isPlaying==YES) { NSLog(@"It's playing"); } } </code></pre> <p>}</p> <p>I hopše this makes it clear what I do. Any suggestions where I might be wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload