1

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

这是一些需要更正的代码:

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

有任何想法吗?

4

1 回答 1

0

一个问题是你正在调用URLWithString,传递它 a NSURL,但它期待 a NSString

例如,考虑以下行:

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

这里有两个问题。首先,在第一行中,outputFileURL已经是 a ,因此在创建对象时NSURL调用是没有意义的。已经是 a ,所以不需要转换。其次,您在第二行中有另一个多余的调用,您正在使用,这已经是 a (或者如果第一行成功,将会是)。URLWithStringtheURLoutputFileURLNSURLURLWithStringtheURLNSURL

您不需要这两个URLWithString调用中的任何一个。它可能应该只是:

NSData *theData = [NSData dataWithContentsOfURL:outputFileURL];

您应该只URLWithString在尝试将 a 转换NSString为 a时调用NSURL。但是如果你已经有一个NSURL对象,你永远不会调用它。

于 2013-08-04T01:26:44.663 回答