gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:51:46 29 4
gpt4 key购买 nike

您好,我需要开发一个可以录制、播放、停止和覆盖音频的应用程序。我使用 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. 将 slider 位置移动到特定点
  3. 然后,开始录音。

这是覆盖之前录音的功能。

所以,按照我写的步骤

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

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

最佳答案

创建 audioSession 对象,由您决定是要激活还是停用应用的 Audio Session 。( 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] ;应定期调用,以刷新录音机所有 channel 的平均和峰值功率值。

你可以为此使用计时器。

例如:

 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 下载示例代码。 .

关于iphone - iOS - 在特定时间覆盖特定录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176238/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com