iphone - 如何为 iPhone 应用程序循环播放声音?

标签 iphone objective-c ios ipad

我正在尝试循环播放我的声音文件,但我找不到循环播放声音文件的参数!我要把头上最后一根头发拔掉。它加载正常......它播放......我只是想不出如何让它循环的代码。

这是我的代码:

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

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSSound/loops

我对如何实现这个感到困惑。

最佳答案

这是对我有用的代码。如果有人需要帮助,请告诉我。 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/

相关文章:

iphone - Objective-c 中的 (void)^(void)?

iphone - 从自定义 UITableViewCell 中的 UITextFields 中检索值

ios - 在项目中导航

ios - GPUImage2 , 前后摄像头切换

objective-c - 如何使用 AnyClass 对象中定义的类型在 Swift 中声明变量?

ios - UITextView 样式在设置文本属性后被重置

ios - 新的 Safari UserAgent - 末尾的 6 位数字

iphone - 从 AVAudioRecorder 播放后 AVAudioPlayer 崩溃

ios - 在 Swift 中记录 CLLocationPoints 时不准确

objective-c - 其他 : {'EXIT' , {错误、超时、#Ref<0.0.0.415>}}