我正在尝试循环播放我的声音文件,但我找不到循环播放声音文件的参数!我要把头上最后一根头发拔掉。它加载正常......它播放......我只是想不出如何让它循环的代码。
这是我的代码:
NSString *eyeBGPath = [[NSBundle mainBundle] pathForResource:@"theeye" ofType:@"caf"];
CFURLRef eyeBGURL = (CFURLRef ) [NSURL fileURLWithPath:eyeBGPath];
AudioServicesCreateSystemSoundID(eyeBGURL, &eyeBackGroundID);
AudioServicesPlaySystemSound(eyeBackGroundID);
我在这里做错了什么?
我发现了这个:
循环 指示接收器在到达其内容末尾时是否重新开始播放。默认值:否。
- (BOOL)loops
Return Value
当接收器在完成后重新开始播放时为 YES,否则为 NO。
可用性 适用于 Mac OS X v10.5 及更高版本。 也可以看看 – 设置循环: 声明于 NSSound.h
我对如何实现这个感到困惑。
最佳答案
这是对我有用的代码。如果有人需要帮助,请告诉我。 Apple Dev 论坛上有人向我指出了 AVFoundation 文档。
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/theeye3.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
这是根据图像数组中的随机数挑选声音文件的代码。
NSString *audioWiz = [[NSString alloc]initWithFormat:@"%d.caf", randNum];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:[@"%@/" stringByAppendingString:audioWiz], [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 0.1;
[audioPlayer play];
关于iphone - 如何为 iPhone 应用程序循环播放声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6653129/