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

时间:2012-03-20 17:03:03

标签: iphone objective-c ios

我在iOS中发现了一些有关播放声音的相互矛盾的数据。我想知道是否有人能指出我每次用户触摸屏幕时只播放一个简单的“ping”声音的最佳方式。

谢谢!

5 个答案:

答案 0 :(得分:29)

我用这个:

标题文件:

#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实例并直接调用方法play。

答案 1 :(得分:25)

这是在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);

答案 2 :(得分:10)

(对正确处理音频的正确答案进行小修改)

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);
});

答案 3 :(得分:2)

您可以使用AVFoundationAudioToolbox

以下是两个分别使用库的示例。

答案 4 :(得分:0)

这是Swift(4)的更新答案:

import AudioToolbox

func playSound() {
    var soundId: SystemSoundID = 0
    guard let soundPath = Bundle.main.url(forResource: "Success7", withExtension: "wav") else {
        print("Error finding file")
        return
    }

    let error = AudioServicesCreateSystemSoundID(soundPath as CFURL, &soundId)
    if error != kAudioServicesNoError {
        print("Error loading sound")
        return
    }

    AudioServicesPlaySystemSoundWithCompletion(soundId) {
        AudioServicesDisposeSystemSoundID(soundId)
    }
}

如果您想要在视图中多次播放声音效果,则可以更聪明地加载和处理音频:

class YourViewController: UIViewController {
    fileprivate lazy var soundId: SystemSoundID? = {
        guard let soundPath = Bundle.main.url(forResource: "Success7", withExtension: "wav") else {
            return nil
        }

        var soundId: SystemSoundID = 0
        let error = AudioServicesCreateSystemSoundID(soundPath as CFURL, &soundId)
        if error != kAudioServicesNoError {
            return nil
        }

        return soundId
    }()

    func playScannedSound() {
        guard let soundId = self.soundId else {
            return
        }

        AudioServicesPlaySystemSoundWithCompletion(soundId, nil)
    }

    deinit {
        guard let soundId = self.soundId else {
            return
        }
        AudioServicesDisposeSystemSoundID(soundId)
    }

}