Ios AVFoundation:在播放循环之间添加静音暂停
我想将录制的声音循环3次,但循环之间需要一秒钟的静音,我该怎么做?我的播放按钮代码:Ios AVFoundation:在播放循环之间添加静音暂停,ios,xcode,avfoundation,avaudioplayer,Ios,Xcode,Avfoundation,Avaudioplayer,我想将录制的声音循环3次,但循环之间需要一秒钟的静音,我该怎么做?我的播放按钮代码: -(IBAction) play_button_pressed{ AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error]; [avPlayer setNumberOfLoops: 2]; [avPlayer play]; } 1) 我能为这个方法添加
-(IBAction) play_button_pressed{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[avPlayer setNumberOfLoops: 2];
[avPlayer play];
}
1) 我能为这个方法添加什么来增加这一秒的沉默
2) 如果没有,有没有办法在实际录音中增加一秒钟的沉默
编辑:谢谢;我得到了一个解决方案,用2秒的停顿重复声音一次;它不会无限循环,你能告诉我我错过了什么吗
-(IBAction) play_button_pressed{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
//[avPlayer setNumberOfLoops: 2];
avPlayer.delegate = self;
[avPlayer prepareToPlay];
[avPlayer play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)avPlayer successfully:(BOOL)flag
{
NSLog(@"audioPlayerDidFinishPlaying");
tm = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(waitedtoplay)
userInfo:nil
repeats:NO];
}
-(void) waitedtoplay
{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[tm invalidate];
[avPlayer prepareToPlay];
[avPlayer play];
NSLog(@"waitedtoplay");
}
据我所知,AVAudioPlayer的任何方法都不会在播放循环之间造成“沉默” 当我进行同样的循环时,我使用了AVAudioPlayerDelegate和timer中定义的回调方法 在头文件中,声明使用AVAudioPlayerDelegate,如下所示
@interface xxxxController : UIViewController
<AVAudioPlayerDelegate>{
并且,在方法“waitedoplay”中,调用next[avPlayer play]
-(void) waitedtoplay{
[tm invalidate];
[avPlayer play];
}
这是一个永无止境的循环,因此请添加计数器以限制循环数=3
编辑
在添加为“waitedoplay”的方法中,您没有设置“avplayer.delegate=self”。
因此,AVAudioPlayer不会称为“AudioPlayerDifinishPlaying”
-(无效)等待托付
{
AVAudioPlayer*avPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:RecordedTempFile错误:&错误];
[tm失效];
avPlayer.delegate=self;
-(void) waitedtoplay{
[tm invalidate];
[avPlayer play];
}
-(void) waitedtoplay
{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[tm invalidate];
avPlayer.delegate = self; <<=== MISSING !
[avPlayer prepareToPlay];
[avPlayer play];
NSLog(@"waitedtoplay");
}