将音频文件录制为mp3或wav

时间:2012-02-16 13:22:55

标签: iphone ios ipad avaudiorecorder

我正在尝试录制和保存mp3文件,因此我使用以下代码

NSString* d = @" ar/mobile/Applications/86F15C5E-66EC-4071-95EB-96CDE64B5948/Library/Application Support/P/5720/5720/Report/Report0.mp3"

NSURL *soundFileURL = [NSURL fileURLWithPath:d];

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

并开始记录我使用

[audioRecorder record];

for mp3我收到了错误 错误:无法完成操作。 (OSStatus错误1718449215。)

如果我将其保存为wav则不记录任何内容,文件为0字节

我错过了什么,不知道如何解决这个问题

2 个答案:

答案 0 :(得分:1)

你没有显示Dpath的定义方式......

NSString * filePath = [NSHomeDirectory()stringByAppendingPathComponent: [NSString stringWithFormat:@"Documents/%@.mp3",songTitle]];

// more code    

recorder = [[AVAudioRecorder alloc] initWithURL: [NSURL fileURLWithPath:filePath settings: recordSettings error: nil];

答案 1 :(得分:0)

也许把它保存到文件夹?这不是正确的法律途径吗?