简体   繁体   English

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

[英]iOS - Overwrite the particular audio recording In specific time

Hi I need to develop an app that can record, play, stop and overwrite audio. 嗨,我需要开发一个可以记录,播放,停止和覆盖音频的应用程序。 I have done the Recording by using AvAudioRecorder : 我已经使用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];

...and playback is done using AVplayer . ...然后使用AVplayer进行播放。

But I dont know how to overwrite the recording. 但是我不知道如何覆盖录音。 This the outline of my overwrite implementation: 这是我覆盖实现的概述:

  1. Stop Recording 停止录音
  2. Move the slider position to the particular point 将滑块位置移动到特定点
  3. Then, start recording. 然后,开始录制。

This is the functionality to overwrite the previous recordings. 这是覆盖先前录音的功能。

So, As per the steps I have written 所以,按照我写的步骤

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

...but its not working. ...但是它不起作用。 Instead, they totally re-record the file. 相反,他们完全重新记录了文件。 Could any body help me for this critical situation. 在这种紧急情况下,谁能帮助我。

Create audioSession object,its upto you whether you want to activate or deactivate your app's audio session.( AVAudioSession ) 创建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];

Start recording proces, 开始录制过程,

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

Pause recording: 暂停录音:

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

Again resume the process..audiosession will do it for you... 再次恢复该过程..audiosession将为您完成...

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

EDIT : [recorder updateMeters] ; 编辑 :[记录器updateMeters] should be called periodically which refreshes the average and peak power values for all channels of an audio recorder. 应该定期调用,以刷新录音机所有通道的平均和峰值功率值。

You can use timer for that. 您可以使用计时器。

For example: 例如:

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

You can download the sample code from here . 您可以从此处下载示例代码。

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

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