OpenAL vs AVAudioPlayer与其他播放声音的技巧相比

时间:2015-02-02 21:07:19

标签: sprite-kit avaudioplayer openal skaction

我知道OpenAL是快速库,但它不支持任何压缩音频格式,并且它不是那么容易使用......

AVAudioPlayer不是那么快,但支持广泛的文件格式,以及像mp3这样的压缩格式。

还有一个可以播放声音的SKAction类,以及SystemSoundID ......

我几乎没有问题:

  1. 什么是首选的方式/玩家/技术:

    • 音效(当时多个)?
    • 声音效果,有时可在小时间延迟后重复
    • 循环播放的背景音乐
  2. 此外,使用未压缩音频进行音效是否明智?我想这没关系,因为那些文件的尺寸都很小?

3 个答案:

答案 0 :(得分:4)

我个人使用ObjectAL。这很好,因为它利用了OpenAL和AVAudioPlayer,但是抽象了很多复杂的部分而不是你。我的游戏有背景音乐,同时播放大量声音,以及基于精灵速度增加音量,音高等的可循环声音。 ObjectAL可以完成所有这些。

ObjectAL可用于播放简单的声音和音乐循环,使用它的OALSimpleAudio类。或者你可以深入研究它并做更复杂的事情。

我已经专门为我的游戏创建了一个围绕ObjectAL的简单包装器,因此它进一步抽象出来。

根据我的阅读,未压缩的音频更好。你只需要确保预加载声音,这样你的游戏就不会在每次播放声音时都试图拉出文件。

答案 1 :(得分:3)

这个非常简单的课程为我提供了多个项目,完美地同时运行了大量的声音。我发现它比使用OpenAL烦恼要简单得多。它包含您要求的一切,预先设置的声音,多个并发播放,延迟后,背景循环。

无论您是否使用压缩文件都无关紧要,因为您首先进行了设置。

SKAudio.h

#import <Foundation/Foundation.h>
@import AVFoundation;

@interface SKAudio : NSObject

+(AVAudioPlayer*)setupRepeatingSound:(NSString*)file volume:(float)volume;
+(AVAudioPlayer*)setupSound:(NSString*)file volume:(float)volume;
+(void)playSound:(AVAudioPlayer*)player;
+(void)playSound:(AVAudioPlayer*)player afterDelay:(float)delaySeconds;
+(void)pauseSound:(AVAudioPlayer*)player;

@end

SKAudio.m

#import "SKAudio.h"

@implementation SKAudio

#pragma mark -
#pragma mark setup sound

// get a repeating sound
+(AVAudioPlayer*)setupRepeatingSound:(NSString*)file volume:(float)volume {
    AVAudioPlayer *s = [self setupSound:file volume:volume];
    s.numberOfLoops = -1;
    return s;
}

// setup a sound
+(AVAudioPlayer*)setupSound:(NSString*)file volume:(float)volume{
    NSError *error;
    NSURL *url = [[NSBundle mainBundle] URLForResource:file withExtension:nil];
    AVAudioPlayer *s = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    s.numberOfLoops = 0;
    s.volume = volume;
    [s prepareToPlay];
    return s;
}

#pragma mark sound controls

// play a sound now through GCD
+(void)playSound:(AVAudioPlayer*)player {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [player play];
    });
}

// play a sound later through GCD
+(void)playSound:(AVAudioPlayer*)player afterDelay:(float)delaySeconds {

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delaySeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [player play];
    });
}

// pause a currently running sound (mostly for background music)
+(void)pauseSound:(AVAudioPlayer*)player {
    [player pause];
}

@end

在游戏中使用:

设置一个类变量并用声音预加载它:

static AVAudioPlayer *whooshHit;
static AVAudioPlayer *bgMusic;

+(void)preloadShared {

    // cache all the sounds in this class 
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

       whooshHit = [SKAudio setupSound:@"whoosh-hit-chime-1.mp3" volume:1.0];

       // setup background sound with a lower volume
       bgMusic = [SKAudio setupRepeatingSound:@"background.mp3" volume:0.3];

    });

}

...


// whoosh with delay
[SKAudio playSound:whooshHit afterDelay:1.0];

...

// whoosh and shrink SKAction
SKAction *whooshAndShrink = [SKAction group:@[
             [SKAction runBlock:^{ [SKAudio playStarSound:whooshHit afterDelay:1.0]; }],
             [SKAction scaleTo:0 duration:1.0]]];

...

[SKAudio playSound:bgMusic];

... 

[SKAudio pauseSound:bgMusic];

答案 2 :(得分:2)

这是@ patrick对Swift 3的解决方案的一个端口。

import AVFoundation

// MARK -
// MARK setup sound

// get a repeating sound
func setupRepeatingSound(file: String, volume: Float) -> AVAudioPlayer? {
    let sound: AVAudioPlayer? = setupSound(file: file, volume: volume)
    sound?.numberOfLoops = -1
    return sound
}

// setup a sound
func setupSound(file: String, volume: Float) -> AVAudioPlayer? {
    var sound: AVAudioPlayer?
    if let path = Bundle.main.path(forResource: file, ofType:nil) {
        let url = NSURL(fileURLWithPath: path)
        do {
            sound = try AVAudioPlayer(contentsOf: url as URL)
        } catch {
            // couldn't load file :(
        }
    }
    sound?.numberOfLoops = 0
    sound?.volume = volume
    sound?.prepareToPlay()
    return sound
}

// MARK sound controls

// play a sound now through GCD
func playSound(_ sound: AVAudioPlayer?) {
    if sound != nil {
        DispatchQueue.global(qos: .default).async {
            sound!.play()
        }
    }
}

// play a sound later through GCD
func playSound(_ sound: AVAudioPlayer?, afterDelay: Float) {
    if sound != nil {
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(afterDelay)) {
            sound!.play()
        }
    }
}

// pause a currently running sound (mostly for background music)
func pauseSound(_ sound: AVAudioPlayer?) {
    sound?.pause()
}