在iPhone应用程序不同声音之间的延时 [英] Latency between different sounds in iPhone application

查看:219
本文介绍了在iPhone应用程序不同声音之间的延时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与一些按钮的小iPhone应用程序来播放WAV声音。
我的按钮的工作原理,但我有一个小的延迟(0.5〜秒)。

I’m trying to make a small iPhone application with some buttons to play WAV sounds. My buttons works, but I have a small latency (~ 0,5 sec).

这是我的.m文件:

#import "buttonSoundViewController.h"

@implementation buttonSoundViewController
//@synthesize player;

-(IBAction) playSoundA:(id)sender{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

-(IBAction) playSoundB:(id)sender{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"b" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player release];
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
}

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
}

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [audioPlayer release];
    [super dealloc];
}   

@end

我怎样才能避免播放不同的声音之间的延迟?

How can I avoid this latency between playing different sounds?

推荐答案

使用AudioServices确实是小声音(小于30秒)快得多。所需code是不是很长,无论是(但它需要一些很好的旧C)。

Using AudioServices is indeed much quicker for small sounds (less than 30 seconds). The required code isn't very long either (but it requires some good old C).

#import <AudioToolbox/AudioServices.h>

SystemSoundID soundID = 0;
NSString* str =  [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
CFURLRef soundFileURL = (CFURLRef)[NSURL URLWithString:str ];
OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);
if (errorCode != 0) {
    // Handle failure here
}
else
    AudioServicesPlaySystemSound(soundID);

你也可以用下面的命令终端优化您的声音(减少大小):

Also you can optimize your sounds (reduce their size) with the following terminal command:

afconvert mysound.caf mysoundcompressed.caf -d ima4 -f caff

这篇关于在iPhone应用程序不同声音之间的延时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆