Iphone 将声音文件上载到服务器
我正在尝试将已录制的m4a格式的声音文件上载到web服务器 下面是一些需要更正的代码:Iphone 将声音文件上载到服务器,iphone,audio,Iphone,Audio,我正在尝试将已录制的m4a格式的声音文件上载到web服务器 下面是一些需要更正的代码: - (void)viewDidLoad { [super viewDidLoad]; // Disable Stop/Play button when application launches [stopButton setEnabled:NO]; [playButton setEnabled:NO]; // Set the audio file pathC
- (void)viewDidLoad
{
[super viewDidLoad];
// Disable Stop/Play button when application launches
[stopButton setEnabled:NO];
[playButton setEnabled:NO];
// Set the audio file
pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
- (IBAction)recordPauseTapped:(id)sender {
// Stop the audio player before recording
if (player.playing) {
[player stop];
}
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
[recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
} else {
// Pause recording
[recorder pause];
[recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
}
[stopButton setEnabled:YES];
[playButton setEnabled:NO];
}
- (IBAction)playTapped:(id)sender {
if (!recorder.recording){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player play];
}
}
-(IBAction)uploadSound {
NSURL *theURL = [NSURL URLWithString:outputFileURL];
NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURL]];
NSString *urlString = @"http://drewgarcia23.3owl.com/upload.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".mov\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
除了上传到服务器外,一切正常。我得到这个错误:
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL长度]:未识别的选择器已发送到实例0x7a9a590”
这与IBAction uploadSound中的代码有关:
有什么想法吗?一个问题是,您正在调用URLWithString,向它传递一个NSURL,但它需要一个NSString 例如,考虑线条:
NSURL *theURL = [NSURL URLWithString:outputFileURL];
NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURL]];
这里有两个问题。首先,在第一行中,outputFileURL已经是一个NSURL,因此在创建theURL对象时调用URLWithString没有意义。outputFileURL已经是NSURL,因此不需要进行转换。第二,在第二行中有另一个冗余的URLWithString调用,在第二行中,您使用的URL已经是NSURL,或者如果第一行成功,将是NSURL
这里不需要这两个URLWithString调用中的任何一个。它可能只是:
NSData *theData = [NSData dataWithContentsOfURL:outputFileURL];
只有在尝试将NSString转换为NSURL时,才应调用URLWithString。但是,如果您已经有一个NSURL对象,就永远不会调用它。另外,我不建议在主队列上执行同步网络请求。在处理网络请求时,您的应用程序可能会失去响应,如果时间过长,在极端情况下,看门狗进程甚至可能会杀死您的应用程序。我建议采用某种形式的异步网络上传过程,例如sendAsynchronousRequest。
NSData *theData = [NSData dataWithContentsOfURL:outputFileURL];