回答
这是打一个简单的声音在iOS中(不超过30秒)的最佳方式:
//Retrieve audio file
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"m4a"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
SystemSoundID audioEffect;
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
// call the following function when the sound is no longer used
// (must be done AFTER the sound is done playing)
AudioServicesDisposeSystemSoundID(audioEffect);
我用这个:
头文件:
#import <AudioToolbox/AudioServices.h>
@interface SoundEffect : NSObject
{
SystemSoundID soundID;
}
- (id)initWithSoundNamed:(NSString *)filename;
- (void)play;
@end
源文件:
#import "SoundEffect.h"
@implementation SoundEffect
- (id)initWithSoundNamed:(NSString *)filename
{
if ((self = [super init]))
{
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
if (fileURL != nil)
{
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
if (error == kAudioServicesNoError)
soundID = theSoundID;
}
}
return self;
}
- (void)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
- (void)play
{
AudioServicesPlaySystemSound(soundID);
}
@end
您将需要创建一个SoundEffect实例并直接调用该方法。
这太好了。我使用的是SystemSoundID的C数组,但是我只是碰到了schlepp需要处理的一点。切换到基于此的东西。谢谢! – 2012-10-19 23:46:42
虽然这不适用于ARC。为了在ARC中使用它,你必须添加一个完成回调函数,你可以在这里处理systemsound。如果你在dealloc中这样做,声音立即死亡: 'AudioServicesAddSystemSoundCompletion(soundID,NULL,NULL,completionCallback,(__bridge_retained void *)self);' 像这样的例子 – Maverick1st 2013-04-08 15:07:32
@ Maverick1st这与ARC非常吻合,必须确保你的'SoundEffect'对象不会立即被释放,比如将它分配给一个属性。 – shawkinaw 2013-07-26 22:15:03
(小修改正确的答案照顾音频的处置)
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"m4a"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
SystemSoundID audioEffect;
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
// Using GCD, we can use a block to dispose of the audio effect without using a NSTimer or something else to figure out when it'll be finished playing.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
AudioServicesDisposeSystemSoundID(audioEffect);
});
这是异步播放声音的最佳方式。 – alones 2014-12-11 02:19:46
- 1. 简单播放iOS中的声音
- 2. 在浏览器中循环播放音频的最佳方式?
- 3. 在Android中播放音频RTMP流最简单的方法
- 4. 单击按钮时播放声音的最有效方式
- 5. 在iOS中播放来自Url的视频的最佳方式
- 6. 播放单音音频文件的最佳方法是什么?
- 7. 在iPad上实现音频播放的最佳方式
- 8. 在iPhone上播放声音的最简单方法是什么?
- 9. 在iOS上播放音符或简单的声音?
- 10. 在iOS游戏中播放声音的最方便方法?
- 11. 最佳流式播放方式
- 12. 使用HTML5和Javascript播放声音的最佳方式
- 13. 实现音频播放的最佳方式
- 14. 什么是通过Qt播放音频的最佳方式?
- 15. 声音播放完毕后执行代码的最佳方式
- 16. Objective-c/IOS:向后播放音频文件最简单的方法是什么
- 17. 同时播放两个声音的最简单方法(C++ winapi)
- 18. 播放循环声音最简单的方法是什么?
- 19. 从C/C++播放声音最简单的方法
- 20. FMOD中的简单声音播放
- 21. 在iOS中以更高音调播放音乐的最简单方法是什么?
- 22. 播放音效在Java中
- 23. 在简单的iOS游戏中,音频的最佳方法是什么?
- 24. 在java中每x秒播放声音的简单方法
- 25. iOS中的简单低延迟音频播放Swift
- 26. 在Haskell中播放声音样本的最简单方法是什么?
- 27. 在Qt中播放通知(频率x)声音 - 最简单的方法?
- 28. 在UWP中播放背景音频最简单的方法是什么?
- 29. WP7:在XNA中播放系统声音的最简单方法是什么
- 30. 在定时间隔播放声音的最佳方法?
我只是做这个自己。 – 2012-03-20 21:18:29
谢谢!有效! – noloman 2013-08-22 10:15:27
谢谢兄弟!其作品.. – 2015-01-05 05:54:51