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 に答える