0

你好,我是 iOS 的新手,我有一个特殊的任务,首先我用 AVAudioRecorder 录制声音,我可以毫无困难地录制和播放它,但我需要在记录中获取数据,这是我的功能:

@interface recordViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
{
    AVAudioRecorder *audioRecorder;
    AVAudioPlayer *audioPlayer;
    UIButton *playButton;
    UIButton *recordButton;
    UIButton *stopButton;
}

记录:

 -(void) recordAudio {
     if (!audioRecorder.recording)
     {
         playButton.enabled = NO;
         stopButton.enabled = YES;
         [audioRecorder record];
     }
}

玩:

-(void) playAudio {
    if (!audioRecorder.recording)
    {
        stopButton.enabled = YES;
        recordButton.enabled = NO;

        NSError *error;

        audioPlayer = [[AVAudioPlayer alloc] 
                       initWithContentsOfURL:audioRecorder.url
                       error:&error];

        audioPlayer.delegate = self;

        if (error)
            NSLog(@"Error: %@", [error localizedDescription]);
        else
            [audioPlayer play];
    } 
}

我怎样才能在记录中获取数据?我在 iOS Developer API 中搜索,但我找不到任何东西,在此先感谢。

4

1 回答 1

0

我还没有尝试过这个解决方案。但这可能会有所帮助。试试看。

录制音频:

itsVoiceRecordedURL = [NSURL fileURLWithPath:[NSString stringWithFormat:<Path of the recorded file to be stored>]];
    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];

    itsAudioRecorder = [[ AVAudioRecorder alloc] initWithURL:itsVoiceRecordedURL settings:recordSetting error:&error];
    [recordSetting release];
    [itsAudioRecorder setDelegate:self];
    //We call this to start the recording process and initialize 
    //the subsstems so that when we actually say "record" it starts right away.
    [itsAudioRecorder prepareToRecord];
    //Start the actual Recording
    [itsAudioRecorder record];

将录制的音频文件转换为字节数组

NSData *data = [NSData dataWithContentsOfFile:AudioFilePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
于 2012-05-29T04:31:06.097 回答