gpt4 book ai didi

iphone - 在 iOS 中播放简单音效的最佳方式

转载 作者:IT王子 更新时间:2023-10-29 08:04:26 29 4
gpt4 key购买 nike

我发现了一些关于在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com