在iPhone应用程序中播放循环声音的最简单方法是什么?播放循环声音最简单的方法是什么?
回答
可能最简单的解决方案是使用AVAudioPlayer,而numberOfLoops:
设置为负整数。
// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>
...
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample_name" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}
// *** In your dealloc... ***
[testAudioPlayer release];
您还应该记得设置适当的音频类别。 (请参阅AVAudioSessionsetCategory:error:
方法。)
最后,您需要将AVFoundation库添加到您的项目中。为此,请在Xcode的Groups & Files列中单击您的项目目标,然后选择“Get Info”。然后选择常规选项卡,单击底部“链接库”窗格中的+并选择“AVFoundation.framework”。
这仍然适用于弧? – 2015-09-28 08:37:18
最简单的方法是使用AVAudioPlayer设置为无限数量的循环(或有限的,如果这是你所需要的)。
喜欢的东西:
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourAudioFileName" ofType:@"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *_player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
_player.numberOfLoops = -1;
[_player prepareToPlay];
[_player play];
任何音频文件已无限期指定这会简单循环。 如果您想要音频文件循环的次数有限,请将循环数设置为任意正整数。
希望这会有所帮助。
干杯。
我得到错误:“使用未声明的标识符'AVAudioPlayer'” – Linuxmint 2011-03-08 19:12:08
请确保您的文件顶部或头文件中有#import
@Brenton不是在'#import
- 1. 从delphi的数组数据播放声音最简单的方法是什么
- 2. 播放单音音频文件的最佳方法是什么?
- 3. 播放ogg循环声音
- 4. 在WPF中播放MP3最简单的方法是什么?
- 5. 什么是自动播放声音最“兼容”的方式?
- 6. 什么是最流畅的音频API循环iPhone的声音
- 7. 在iPhone应用程序中播放声音剪辑的最简单方法是什么?
- 8. 简单的方法来播放声音文件(.aif)
- 9. 简单播放iOS中的声音
- 10. 播放背景声音循环
- 11. 在WP7中播放声音循环
- 12. MediaPlay无法播放声音,为什么?
- 13. 什么是最低级别的Windows功能播放声音?
- 14. 在Android中播放音频RTMP流最简单的方法
- 15. 在iOS中以更高音调播放音乐的最简单方法是什么?
- 16. 播放较长声音片段的标准方式是什么?
- 17. 从wifi摄像头播放视频最简单的方法是什么?
- 18. PulpCore音乐播放 - 循环用声音和动画音量
- 19. 比较简单的声音 - 什么是最接近的频率
- 20. 在iOS游戏中播放声音的最方便方法?
- 21. 什么是嵌套for循环最大的CPU使用最简单的方法是什么?
- 22. 在浏览器中循环播放音频的最佳方式?
- 23. 什么是通过Qt播放音频的最佳方式?
- 24. 声音为什么不播放?
- 25. 使用C#从RTMP服务器获取和播放音频流的最简单方法是什么?
- 26. 单击按钮时播放声音的最有效方式
- 27. Python:播放声音样本的时间最准确的方法
- 28. 循环连续播放多个单曲音频播放器
- 29. 在iOS中播放简单音效的最佳方式
- 30. 播放声音
@Federico谢谢你的见解。 :-) – 2011-03-08 19:21:15
从现在起称我为Captain Obvious。我希望这很明显,我在开玩笑:-) – 2011-03-08 20:00:46
@Federico(或者应该是@Captain?)是的,我会说这很明显,你是在开玩笑。 :-) – 2011-03-08 20:03:07