首页 文章

iOS - 在特定时间覆盖特定的音频录制

提问于
浏览
2

嗨,我需要开发一个可以录制,播放,停止和覆盖音频的应用程序 . 我使用 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 完成播放 .

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

  • 停止录制

  • 将滑块位置移动到特定点

  • 然后,开始录制 .

这是覆盖以前录制内容的功能 .

所以,按照我写的步骤

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

......但它不起作用 . 相反,他们完全重新录制文件 . 在这种危急情况下,任何身体都能帮助我 .

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

    再次恢复过程......声明会为你做...

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

    EDITLogger [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
    }
    }
    }
    

    您可以从here下载示例代码 .

相关问题