将AVAudioRecorder发送到服务器iOS

编程入门 行业动态 更新时间:2024-10-26 11:29:38
本文介绍了将AVAudioRecorder发送到服务器iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在过去几天尝试将一个本地保存的sounde记录上传到服务器(用一个php文件处理它并将其保存到服务器)。 问题是我找不到办法做到这一点。

I am trying for the past few days to upload a locally saved sounde record to a server (which handels it with a php file and saves it to the server). the problem is that I can't find a way to do that.

录制声音(AVAudioRecorder)时,它保存在NSTemporaryDirectory() :

While recording the sound (AVAudioRecorder) it saves at a "NSTemporaryDirectory()":

NSURL *temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"VoiceFile"]]];

现在,我检查答案的地方是使用 ASIHTTPRequest 。 ,但问题是它在ios6中不受支持。

Now, everywhere I checked the answer was to use ASIHTTPRequest. but the problem is that it is not supported in ios6.

还有一些其他的库,但无论我尝试什么和如何都没有任何作用。 我很乐意,如果有人可以帮助我...

There are some other libraries for it but nothing works no matter what and how I try. I would love it if someone could help me...

这是我的代码:

ViewController.h

#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <CoreAudio/CoreAudioTypes.h> @interface ViewController : UIViewController <AVAudioRecorderDelegate,AVAudioPlayerDelegate> { UIButton *recordButton; UIButton *playButton; UIButton *uploadFile; UILabel *recStateLabel; BOOL isNotRecording; NSURL *temporaryRecFile; AVAudioRecorder *recorder; AVAudioPlayer *player; } @property (nonatomic,retain) IBOutlet UIButton *uploadFile; @property (nonatomic,retain) IBOutlet UIButton *playButton; @property (nonatomic,retain) IBOutlet UIButton *recordButton; @property (nonatomic,retain) IBOutlet UILabel *recStateLabel; -(IBAction)recording; -(IBAction)playback; -(IBAction)upload; @end

ViewController.m

#import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import "STHTTPRequest.h" @interface ViewController () @end @implementation ViewController @synthesize recordButton,playButton,uploadFile,recStateLabel; -(IBAction)recording { if(isNotRecording) { isNotRecording = NO; [recordButton setTitle:@"Stop" forState:UIControlStateNormal]; playButton.hidden = YES; recStateLabel.text = @"Recording"; temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"VoiceFile"]]]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, nil]; recorder = [[AVAudioRecorder alloc]initWithURL:temporaryRecFile settings:recordSettings error:nil]; [recorder setDelegate:self]; [recorder prepareToRecord]; [recorder record]; } else { isNotRecording = YES; [recordButton setTitle:@"Rec" forState:UIControlStateNormal]; playButton.hidden = NO; recStateLabel.text = @"Not Recording"; [recorder stop]; } } -(IBAction)playback { player = [[AVAudioPlayer alloc]initWithContentsOfURL:temporaryRecFile error:nil]; //player.volume = 1; [player setVolume: 1.0]; [player play]; } -(IBAction)upload { NSLog(@"upload"); recStateLabel.text = @"Uploading"; NSData *data = [NSData dataWithContentsOfFile:NSTemporaryDirectory()]; NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@""]; [urlString appendFormat:@"%@", data]; NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSString *baseurl = @"efron.il/dev/singsong/upload.php"; NSURL *url = [NSURL URLWithString:baseurl]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setHTTPMethod: @"POST"]; [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [urlRequest setHTTPBody:postData]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self]; [connection start]; NSLog(@"Started!"); /* STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"efron.il/dev/singsong/upload.php"]; rpletionBlock = ^(NSDictionary *headers, NSString *body) { // ... }; r.errorBlock = ^(NSError *error) { NSLog(@"error"); }; [r setFileToUpload:NSTemporaryDirectory() parameterName:@"sound"]; [r startAsynchronous]; ///................../// NSString *boundary = @"---------------------------14737809831466499882746641449"; NSMutableData *postData = [NSMutableData data]; NSString *header = [NSString stringWithFormat:@"--%@\r\n", boundary]; [postData appendData:[header dataUsingEncoding:NSUTF8StringEncoding]]; //add your filename entry NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"filename", @"iosaudio"]; [postData appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData:[NSData dataWithContentsOfFile:NSTemporaryDirectory()]]; NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",boundary]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setHTTPMethod:@"POST"]; [request setURL:[NSURL URLWithString:@"efron.il/dev/singsong/upload.php"]]; [postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postData]; ///.........../// NSURL *url = [NSURL URLWithString:@"efron.il/"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSData *songData = [NSData dataWithContentsOfURL:temporaryRecFile]; NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"dev/singsong/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFormData:songData name:@"file"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { recStateLabel.text = [NSString stringWithFormat:@"Sent %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite]; }]; [httpClient enqueueHTTPRequestOperation:operation]; */ } - (void)viewDidLoad { isNotRecording = YES; playButton.hidden = YES; recStateLabel.text = @"Not Recording"; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [audioSession setActive:YES error:nil]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

ViewController。 m 包括我用图书馆进行的一些测试,例如 STHTTPRequest 和AFNetworking

The ViewController.m includes some test I have made with libraries such as STHTTPRequest and AFNetworking

推荐答案

这行代码对我很有用

现在我发布了我的整个代码用于录制,保存,播放和播放发布到服务器

now I have post my whole code for Record,save,play & post to server

已编辑: -

SoundRecordVC.h

///*********************** SoundRecordVC.h ***************************// #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface SoundRecordVC : UIViewController<AVAudioPlayerDelegate,AVAudioRecorderDelegate> { BOOL stopBtnFlag; BOOL pauseBtnFlag; NSTimer *timer; AVAudioPlayer *audioPlayer; AVAudioRecorder *audioRecorder; } // ------------------- Properties --------------------- @property (weak, nonatomic) IBOutlet UIButton *recordBtn; @property (weak, nonatomic) IBOutlet UIButton *playBtn; @property (strong, nonatomic) AVAudioRecorder *audioRecorder; @property (strong, nonatomic) AVAudioPlayer *audioPlayer; @property (strong,nonatomic) NSString *friendId; // ------------------- IBAction ----------------- - (IBAction)recordBtnPressed:(id)sender; - (IBAction)playBtnPressed:(id)sender;

SoundRecordVC.m

//*********************** SoundRecordVC.m ***************************// #import "SoundRecordVC.h" @interface SoundRecordVC.m () @end @implementation SoundRecordVC.m @synthesize recordBtn; @synthesize playBtn; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - #pragma mark - methods - (IBAction)recordBtnPressed:(id)sender { if (!stopBtnFlag) { if (!audioRecorder.recording) { [self performSelectorOnMainThread:@selector(setUpAudioRecorder) withObject:nil waitUntilDone:YES]; [audioRecorder record]; NSLog(@"Recording..."); } stopBtnFlag = YES; } else { [audioRecorder stop]; stopBtnFlag = NO; } } -(void)setUpAudioRecorder { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; // --------------------- Setting for audio ----------------------// NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,[NSNumber numberWithInt:16],AVEncoderBitRateKey,[NSNumber numberWithInt:2],AVNumberOfChannelsKey,[NSNumber numberWithInt:44100.0],AVSampleRateKey, nil]; NSError *error = nil; // --------------------- File save Path ----------------------// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sounds.caf", documentsDirectory]]; audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error]; audioRecorder.delegate = self; if ([audioRecorder prepareToRecord] == YES){ [audioRecorder prepareToRecord]; }else { int errorCode = CFSwapInt32HostToBig ([error code]); NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); } } - (IBAction)playBtnPressed:(id)sender { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; //----------------------loading files to player for playing------------------------------------// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sounds.caf", documentsDirectory]]; NSError *error; NSLog(@"url is %@",url); [audioPlayer stop]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = 0; audioPlayer.delegate=self; [audioPlayer prepareToPlay]; if(!pauseBtnFlag){ NSLog(@"Playing......."); [audioPlayer play]; pauseBtnFlag=YES; }else{ NSLog(@"Pause"); [audioPlayer pause]; pauseBtnFlag=NO; } } #pragma mark - #pragma mark - AVRecorder Delegate -(void)audioRecorderDidFinishRecording: (AVAudioRecorder *)recorder successfully:(BOOL)flag { NSLog (@"audioRecorderDidFinishRecording:successfully"); NSLog(@"Stopped"); } -(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error { NSLog(@"Encode Error occurred"); } -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { } -(void)dataUploadOnServer { NSLog(@"upload"); recStateLabel.text = @"Uploading"; NSString *baseurl = @"efron.il/dev/singsong/upload.php"; NSURL *dataURL = [NSURL URLWithString:baseurl]; NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [dataRqst setHTTPMethod:@"POST"]; NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; NSMutableData *postBody = [NSMutableData data]; // -------------------- ---- Audio Upload Status ---------------------------\\ //pass MediaType file [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Disposition: form-data; name=\"Data\"; filename=\"myVoice.mp3\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: audio/caf\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //*******************load locally store audio file********************// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *audioUrl = [NSString stringWithFormat:@"%@/record.mp3", documentsDirectory]; // get the audio data from main bundle directly into NSData object NSData *audioData; audioData = [[NSData alloc] initWithContentsOfFile:audioUrl]; // add it to body [postBody appendData:audioData]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // final boundary [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; // add body to post [dataRqst setHTTPBody:postBody]; NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init]; NSError* error = [[NSError alloc] init] ; //synchronous filling of data from HTTP POST response NSData *responseData = [NSURLConnection sendSynchronousRequest:dataRqst returningResponse:&response error:&error]; //convert data into string NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding]; NSLog(@"Response String %@",responseString); } #pragma mark - - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewDidUnload { [self setPlayBtn:nil]; [super viewDidUnload]; }

@end

我希望它适用于你

更多推荐

将AVAudioRecorder发送到服务器iOS

本文发布于:2023-10-10 22:26:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   服务器   AVAudioRecorder   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!