Sachin Bhardwaj

Sachin Bhardwaj

  • Tech Writer
  • 15.4k
  • 1.7m

how to save recorded sound file on table in iphone/ipad

Oct 19 2012 11:45 AM
Hello friends,   
     I am Working on Project, In this Project I was create audio recorder and i use these code for implementing it....
#import "recorderview.h"


#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>




#import "savefileViewController.h"
@interface recorderview ()


@end


@implementation recorderview
@synthesize playbtn,savebtn,stopbtn,recordbtn,progressview,indicator,save;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    //Start the toggle in true mode.
toggle = YES;
    playbtn.enabled = NO;
    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];
   
    error = nil;
   
    audioRecorder = [[AVAudioRecorder alloc]
                    initWithURL:soundFileURL
                    settings:recordSettings
                    error:&error];
   
    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
       
    } else {
        [audioRecorder prepareToRecord];
    }


}
- (IBAction) start_click
{
    NSLog(@"start");
    [indicator startAnimating];
    if (!audioRecorder.recording)
    {
        playbtn.enabled = NO;
        stopbtn.enabled = YES;
        [audioRecorder record];
    }


    progressview.progress = 0.0;
    [audioRecorder recordForDuration:(NSTimeInterval)2 ];
   
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
   
}
- (IBAction) stop_click;
{
    [indicator stopAnimating];
    stopbtn.enabled = NO;
    playbtn.enabled = YES;
    recordbtn.enabled = YES;
   
    if (audioRecorder.recording)
    {
        [audioRecorder stop];
    } else if (audioPlayer.playing) {
        [audioPlayer stop];
    }
 //[timer invalidate];
}
- (IBAction) save_click;
{
    savefileViewController *save1 =[[savefileViewController alloc]init];
    [UIView transitionWithView:self.navigationController.view duration:1
                      options:UIViewAnimationOptionTransitionFlipFromTop
                    animations:^{
                        [self.navigationController pushViewController:save1 animated:NO];
                    }
                    completion:^(BOOL  completed)
    {
         
        //[sourceVC.navigationController pushViewController:destinationVC animated:NO];
    }
    ];
   


    }


- (IBAction) Play_click
{
    [indicator startAnimating];
    if (!audioRecorder.recording)
    {
        stopbtn.enabled = YES;
        recordbtn.enabled = NO;
       
        if (audioPlayer)
            [audioPlayer release];
       
        audioPlayer = [[AVAudioPlayer alloc]
                      initWithContentsOfURL:audioRecorder.url
                      error:&error];
       
        //audioPlayer.delegate = self;
       
        if (error)
            NSLog(@"Error: %@",
                  [error localizedDescription]);
        else
            [audioPlayer play];
    }


}




- (void)handleTimer {
    NSLog(@"progress value%f",progressview.progress);
    progressview.progress +=0.1;
    NSLog(@"check");
   
    if(progressview.progress==1)
    {
      // [timer invalidate];
    }
   
   
   
}




-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
    recordbtn.enabled = YES;
    stopbtn.enabled = NO;
}
-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player
                                error:(NSError *)error
{
    NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:
(AVAudioRecorder *)recorder
                          successfully:(BOOL)flag
{
}
-(void)audioRecorderEncodeErrorDidOccur:
(AVAudioRecorder *)recorder
                                  error:(NSError *)error
{
    NSLog(@"Encode Error occurred");
}
- (void)viewDidUnload
{
   
    audioPlayer = nil;
    audioRecorder = nil;
    stopbtn = nil;
    recordbtn = nil;
    playbtn = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}




- (void)dealloc {
    [audioPlayer release];
    [audioRecorder release];
    [stopbtn release];
    [playbtn release];
    [recordbtn release];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
   
}


@end
Using this code voice record but temporary, i want to save recorded file on table.
Can you suggest how can i save these temporary file on table Because in future when we want to listen it again. It will work...