I want to loop a recorded sound 3 times, but want one second of silence between loops, how can I go about this? My code for play_button:
-(IBAction) play_button_pressed{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[avPlayer setNumberOfLoops: 2];
[avPlayer play];
}
1) Anything I can add to this method to add this one second of silence?
2) If not, is there any way to add a second of silence to the actual recording?
EDIT: Thanks; I have gotten a solution that repeats the sound one time with a pause of 2 seconds; it does not loop infinitely though, can you tell what I am missing?
-(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");
}
As far as I know, no method of AVAudioPlayer makes "silence" between the play loop.
When I made same kind of loop, I utilized call-back method defined in AVAudioPlayerDelegate and timer.
In the header file, declare to use AVAudioPlayerDelegate as follows;
@interface xxxxController : UIViewController
<AVAudioPlayerDelegate>{
When avPlayer ends to play the sound, the method "audioPlayerDidFinishPlaying" will be called.
Then call method to wait certain period of time, for example;
-(void) wait{
float seconds = 2.0f;
tm = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:@selector(waitedtoplay)
userInfo:nil
repeats:NO];
}
And, in the method "waitedtoplay", call next [avPlayer play]
-(void) waitedtoplay{
[tm invalidate];
[avPlayer play];
}
This is a never-ending loop, so please add counter to limit the number of loop = 3.
EDIT
In the method you added as "waitedtoplay", you missed to set "avplayer.delegate = self". So, AVAudioPlayer will not call "audioPlayerDidFinishPlaying".
-(void) waitedtoplay
{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[tm invalidate];
avPlayer.delegate = self; <<=== MISSING !
[avPlayer prepareToPlay];
[avPlayer play];
NSLog(@"waitedtoplay");
}
Please add it as above, and it will repeat infinitely...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.