iphone - 我们如何保存多个具有不同音频数据(声音)和不同名称的音频录制文件(.caf)?

标签 iphone objective-c avaudiorecorder

谁能告诉我如何获取录音数据?我想用不同的数据保存多个录制文件。我正在做的是访问 url 、目录路径并重命名它们,但结果是它们使用不同的名称保存,但不使用不同的数据。所有文件都播放我录制的相同的最后一个声音。

源代码:-

- (IBAction) startRecording
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
[audioSession setMode:AVAudioSessionModeVoiceChat error:&err];

if(err)
{
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

[audioSession setActive:YES error:&err];
err = nil;

if(err)
{
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

recordSetting = [[NSMutableDictionary alloc] init];

// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];

// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

[recordSetting setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSetting setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setObject:[NSNumber numberWithInt: AVAudioQualityMax] forKey: AVEncoderAudioQualityKey];

recorderFilePath = [NSString stringWithFormat:@"%@/My Sound.caf",DOCUMENTS_FOLDER];

url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
soundData = [[NSData alloc] initWithContentsOfURL: url];
NSLog(@"soundData:%@",soundData);
NSLog(@"url:%@",url);

err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder)
{
    NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: [err localizedDescription]
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];

    return;
}

//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;

BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) 
{
    UIAlertView *cantRecordAlert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: @"Audio input hardware not available"
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [cantRecordAlert show];

}

// start recording
[recorder recordForDuration:(NSTimeInterval) 60];
Tag=30;

}


- (IBAction)saveRecording
{ 
if([recordingNameTextField.text isEqualToString: @""])
{
    [alertView setMessage:@"Recording File Name not set.\n\n"];
    [alertView show];
}
else 
{
    recorderFilePath = [NSString stringWithFormat:@"%@/My Sound.caf",DOCUMENTS_FOLDER];
    NSLog(@"%@",recorderFilePath);

    NSLog(@"%@",url);


    renameString =[NSString stringWithFormat:@"%@/%@.caf",DOCUMENTS_FOLDER,recordingNameTextField.text];
    NSLog(@"%@",renameString);

    url2 = [NSURL fileURLWithPath:renameString];
    NSLog(@"%@",url2);

    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *renameError;

    [fm moveItemAtURL:url toURL:url2 error:&renameError];

    [recordingFile checkAndCreateDatabase];
    [recordingFile addRecord:1 andname:recordingNameTextField.text];
    [recordingFile readDatabase];
    recordingNameTextField.text = @"";
    NSLog(@"Text Field: %@",recordingNameTextField.text);
    saveRecording.userInteractionEnabled=NO;
}

请帮忙。谢谢:)

最佳答案

首先在sqlite中创建数据库,然后创建表。下面是插入代码。

[self databasePath];
BOOL checkQuery=NO;

const char *sqlStatement;
sqlStatement = [sql UTF8String];
    blob=voicedata; 

//if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL)!= SQLITE_OK) {
        checkQuery=NO;  
    }
    else {
        sqlite3_bind_blob(statement, Loc, [blob bytes], [blob length], NULL);

        if(SQLITE_DONE != sqlite3_step(statement)) {
            checkQuery=NO;
        }
        else {
           // NSLog(@"Query Working nicely");
            checkQuery=YES; 

            sqlite3_reset(statement);
        }
    }
//}
//sqlite3_close(database);
sqlite3_finalize(statement);

在上面的代码中, sql=查询字符串 Blob =nsdata loc=整数值1 尝试一下,如果有任何说明让我知道,它对我来说效果很好......

关于iphone - 我们如何保存多个具有不同音频数据(声音)和不同名称的音频录制文件(.caf)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10347704/

相关文章:

iphone - 如何正确获取presentationLayer的当前变换?

ios - iOS 中的自定义角标(Badge)问题

ios - 如何创建带有渐变和高亮的 UIButton?

swift - 通过 iPhone 耳机插孔发送数据

ios - 如何通过成功将音频保存到文件来知道 AVAudioRecorder 何时停止?

iphone - 如何使用 QBRTCSession 对象发送自定义字符串

iphone - iPhone 中我的目录路径中的文件夹名称

ios - 将 UINavigationBar 添加到以模态方式呈现的 TableViewController 中?

ios - 将矩阵转换为 UIImage

iphone - 如何使用 AVAudioRecorder 暂停和恢复录音