如何在iOS中使用AVAudioPlayer进行无限重复 [英] How to do unlimited repetition by using AVAudioPlayer in ios
本文介绍了如何在iOS中使用AVAudioPlayer进行无限重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用中,我正在使用音频.我想在点击按钮时无限期播放音频.有可能吗?
In my app i am using audio. I want to play audio for unlimited time on tapping button. Is it possible?
推荐答案
最简单的方法是在播放器到达终点时重新播放:
Easiest way is to re-play the player when it reaches the end :
-(void)startPlayer {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.delegate = self;
if (error) {
NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
} else {
[aPlayer play];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag {
aPlayer.currentTime = 0;
[aPlayer play];
}
调用一次[self startPlayer];
,它将永远循环!
Call [self startPlayer];
once, and it will loop forever !
这篇关于如何在iOS中使用AVAudioPlayer进行无限重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文