简体   繁体   English

如何在目标C(iPhone)中保存录制的音频

[英]How to save a recorded audio in objective C (iPhone)

I am using AV audio framework to record audio files on the iPhone. 我正在使用AV音频框架在iPhone上录制音频文件。 Here is the current code : 这是当前代码:

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

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

Now I have another function and I want to send the sound.caf file to that function something like: 现在,我有另一个功能,我想将sound.caf文件发送到该功能,例如:

NSData *soundData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:soundFilePath ofType:@"caf"]];  

But for some reason that above line seems to return a 0kb file. 但是由于某种原因,上述行似乎返回了0kb的文件。

Any suggestions? 有什么建议么?

In your code, file URL is not global. 在您的代码中,文件URL不是全局的。 You try the code given 您尝试给出的代码

First of all you have to start a new audio session: 首先,您必须开始一个新的音频会话:

-(void) initAudioSession {

    AVAudioSession * audioSession = [AVAudioSession sharedInstance];

        [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];

        [audioSession setActive:YES error: &error];
}

-(void) recordStart {

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docsDir = [dirPaths objectAtIndex:0];

    tempFilePath = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]];


    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]];


    recorder = [[ AVAudioRecorder alloc] initWithURL:tempFilePath settings:recordSetting error:&error];
    [recorder setDelegate:self];

    [recorder prepareToRecord];

    [recorder record];
}

-(void) stopRecording{
    [recorder stop];
}

-(void) playRecord{
    avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:tempFilePath error:&error];
    [avPlayer prepareToPlay];
    [avPlayer play];
}

I don't see you triggering the record method: http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html 我看不到您触发记录方法: http : //developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html

Nor points your file open to the same file name. 也不要将文件打开指向相同的文件名。

Last, you cant save in the app directory. 最后,您无法保存在应用目录中。 You can only save data in the Documents folder. 您只能将数据保存在“文档”文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM