将Sound文件上传到服务器

时间:2013-08-04 00:59:41

标签: iphone audio

我正在尝试将已经采用m4a格式的录制声音文件上传到网络服务器。

以下是一些需要更正的代码:

- (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

中的此代码有关
NSURL *theURL = [NSURL URLWithString:outputFileURL];
NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURL]];

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

一个问题是,您正在调用URLWithString,并将其传递给NSURL,但它预计会有NSString

例如,考虑以下几行:

NSURL *theURL = [NSURL URLWithString:outputFileURL];
NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURL]];

这里有两个问题。首先,在第一行中,outputFileURL已经是NSURL,因此在创建URLWithString对象时调用theURL没有意义。 outputFileURL已经是NSURL,因此无需转换。其次,您在第二行中有另一个冗余URLWithString调用,您正在使用theURL,它已经是NSURL(或者如果第一行成功的话)。

您不需要这两个URLWithString来电。它可能应该只是:

NSData *theData = [NSData dataWithContentsOfURL:outputFileURL];

您只应在尝试将URLWithString转换为NSString时致电NSURL。但是如果你已经拥有NSURL对象,你永远不会打电话。