我发现了一些关于在 iOS 中播放声音的相互矛盾的数据。每次用户触摸屏幕时只播放简单的“ping”声音片段的推荐方法是什么?
最佳答案
我用这个:
头文件:
#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 的实例并直接调用它的播放方法。
关于iphone - 在 iOS 中播放简单音效的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791491/