Friday 1 March 2013

Below is the tutorial on the Audio record and post the audio file on server
Reference tutorial : Here
Below is the coding.

//
//  ViewController.m
//  RecordAudio
//
//  Created by mac on 28/02/13.
//  Copyright (c) 2013 mac. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"
#import "ASIFormDataRequest.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize recordButton,playButton,stopButton,progressview,uploadButton;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    playButton.enabled = NO;
    stopButton.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];
    
    NSError *error = nil;
    
    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];
    
    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [audioRecorder prepareToRecord];
    }

}
-(IBAction) recordAudio
{
    NSLog(@"record audio");
    if (!audioRecorder.recording)
    {
        playButton.enabled = NO;
        stopButton.enabled = YES;
        [audioRecorder record];
    }

}
-(IBAction) playAudio
{
    NSLog(@"audio url=%@",audioPlayer.url);
    
    NSLog(@"play audio");
    if (!audioRecorder.recording)
    {
        stopButton.enabled = YES;
        recordButton.enabled = NO;
        
        NSError *error;
        
        audioPlayer = [[AVAudioPlayer alloc]
                       initWithContentsOfURL:audioRecorder.url
                       error:&error];
        
        audioPlayer.delegate = self;
        
        if (error)
            NSLog(@"Error: %@",
                  [error localizedDescription]);
        else
            [audioPlayer play];
    }
   updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:audioPlayer repeats:YES];
    
    //[progressview setProgress:audioPlayer.currentTime/audioPlayer.duration animated:YES];

}
- (void)updateCurrentTime
{
    if ((int)audioPlayer.currentTime<=(int)audioPlayer.duration)
    {
      [progressview setProgress:audioPlayer.currentTime/audioPlayer.duration animated:YES];  
        
    }
    else
    {
        [updateTimer invalidate];
    }
    
     
}
-(IBAction) stop
{
    NSLog(@"stop audio");
    stopButton.enabled = NO;
    playButton.enabled = YES;
    recordButton.enabled = YES;
    
    if (audioRecorder.recording)
    {
        [audioRecorder stop];
    } else if (audioPlayer.playing) {
        [audioPlayer stop];
    }

}
-(IBAction)uploadAudio:(id)sender
{
    NSLog(@"upload the audio file to the server");
    NSURL *url = [NSURL URLWithString:@"http://192.168.0.100:8888/videoUploads/Upload.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    //[request setData:pVideoData forKey:@"file"];
    [request addPostValue:@"sound.caf" forKey:@"name"];
    request.numberOfTimesToRetryOnTimeout = 3;
    [request setData:[NSData dataWithContentsOfURL:audioPlayer.url] withFileName:@"sound.caf" andContentType:@"multipart/form-data" forKey:@"userfile"];
    //[request addFile:[audioPlayer.url absoluteString]forKey:@"userfile"];
    [request setDelegate:self];
    [request showAccurateProgress];
    [request setRequestMethod:@"POST"];
    //[request setShouldStreamPostDataFromDisk:YES];
    [request setDidFinishSelector:@selector(postRequestSuccess:)];
    [request setDidFailSelector:@selector(postRequestFailed:)];
    [request startAsynchronous];
    NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseString %@",[request responseString]);



}
-(void)postRequestSuccess:(ASIHTTPRequest *)request
{
    NSLog(@"vidoe uplodaded successfully");
    
    NSLog(@"response Description-->%@",[request responseString]);
}
-(void)postRequestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"vidoe uplodaded failed");
    NSError *error = [request error];
    NSLog(@"Error Description-->%@",[error localizedDescription]);
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



Download the source code : Here

No comments:

Post a Comment