我想通过打开开关来播放音频循环,但我不希望循环结束直到开关关闭。当循环结束时,我希望它简单地从头开始作为连续体。
答案 0 :(得分:0)
尝试这样的事情。请注意-1
告诉音乐无限重复:
将此文件导入文件顶部:
#import <AVFoundation/AVFoundation.h>
然后使用它:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:resourcePath ofType:@"caf"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
[newPlayer prepareToPlay];
[newPlayer setVolume:.8];
[newPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[newPlayer setDelegate: self];
[newPlayer play];
[newPlayer release];
然后停止它你会做:
if ([newPlayer isPlaying]) {
[newPlayer stop];
}