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

标签 iphone objective-c ios

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

相关文章:

iphone - Objective C,内存管理

iphone - 有没有办法获得触摸位置的MKMapView的纬度-iPhone SDK

ios - CMSampleBuffer 将 BGRA 转换为 YUV 420f

ios - Firebase 导致 "Thread 1: signal SIGABRT"

ios - 如何在 iOS 模拟器中模拟 iPhone 进入休眠状态?

ios - 为什么我的 iPhone 无法从 Apple Watch 获取心率数据,但 watch 扩展可以?

iphone - 单击 iPhone 应用程序中的表格 View 单元格打开相机

objective-c - 使用 Ruby 而不是 AppleScript 使 Cocoa 应用程序可编写脚本

ios - iOS 7 上的 UINavigationBar Flash

objective-c - 使用 iOS 设备作为 TCP 客户端 - 没有 Bonjour