完成后重启(音频)循环

时间:2011-03-29 19:54:26

标签: iphone loops audio

我想通过打开开关来播放音频循环,但我不希望循环结束直到开关关闭。当循环结束时,我希望它简单地从头开始作为连续体。

1 个答案:

答案 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];
}