3

您好我需要开发一个可以录制、播放、停止和覆盖音频的应用程序。我已经使用以下方法完成了录制AvAudioRecorder

NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithFloat: 44100],AVSampleRateKey,
                              [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
                              [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
                              [NSNumber numberWithInt: AVAudioQualityLow], AVEncoderAudioQualityKey,
                              nil];

self.audioRecorder = [[AVAudioRecorder alloc]
                          initWithURL:audioFileURL
                          settings:audioSettings
                          error:nil];
[sliderTimer invalidate];
[self.audioRecorder record];
sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
                                               target:self    
                                             selector:@selector(updateSlider)
                                             userInfo:nil repeats:YES];

...并且播放是使用AVplayer.

但我不知道如何覆盖录音。这是我的覆盖实现的大纲:

  1. 停止录制
  2. 将滑块位置移动到特定点
  3. 然后,开始录制。

这是覆盖以前记录的功能。

所以,按照我写的步骤

[self.audioRecorder stop];
[[self audioSlider] setValue:self.audioSlider.value animated:YES];
[self.audioRecorder recordAtTime:self.audioSlider.value forDuration:self.audioSlider.maximumValue];

...但它不工作。相反,他们完全重新录制了文件。任何机构都可以帮助我应对这种危急情况。

4

1 回答 1

0

创建 audioSession 对象,由您决定是否要激活或停用应用程序的音频会话。(AVAudioSession

audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

    if(err) {

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

    [audioSession setActive:YES error:&err];

开始录制过程,

-startRecording
{   

[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

    // We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
    [recordSetting setValue:[NSNumber numberWithFloat:32000.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];

mediaPath = [[NSString stringWithFormat:@"%@/myVoice.mp3", DOCUMENTS_FOLDER] retain];//where you want to save your recorded audio.

    NSURL *url = [NSURL fileURLWithPath:mediaPath];
    err = nil;
    NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];

  recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];

[recorder setDelegate:self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;

    BOOL audioHWAvailable = audioSession.inputAvailable;

    if (! audioHWAvailable) {



          UIAlertView *cantRecordAlert = [[UIAlertView alloc] initWithTitle: @"Warning"
                                                                      message: @"Audio input hardware not available"
                                                                     delegate: nil cancelButtonTitle:@"OK"
                                                            otherButtonTitles:nil];
            [cantRecordAlert show];
            [cantRecordAlert release];
            return;
        }
    [recorder record];
}

暂停录音:

-pauseRecording
{
  [recorder pause];
  //[recorder updateMeters];
}

再次恢复该过程..audiosession将为您完成...

-resumerecording
{
  [recorder record];
  //[recorder updateMeters];
}

编辑:[记录器updateMeters];应该定期调用它来刷新录音机所有通道的平均和峰值功率值。

您可以为此使用计时器。

例如:

 NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateAudioDisplay) userInfo:NULL repeats:YES];

-(void)updateAudioDisplay
{
if (!recorder.isRecording)
{
[recorder updateMeters];
}
else
{
 if (recorder == nil) {

   //recording is not yet started
}
else
{
 //paused
}
}
}

您可以从这里下载示例代码。

于 2013-10-04T08:33:00.217 回答