Latency between different sounds in iPhone applica

2019-03-31 19:35发布

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).

This is my .m file :

#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?

5条回答
姐就是有狂的资本
2楼-- · 2019-03-31 19:51

From the Apple Docs grab the SoundEffect class in project BubbleLevel

SoundEffect is a simple Objective-C wrapper around Audio Services functions that allow the loading and playing of sound files.

Classes/SoundEffect.m

this will make playing files as easy as

  SoundEffect *soundEffect = [SoundEffect soundEffectWithContentsOfFile:@""]
  [soundEffect play];

it will also handle memory deallocation. AudioServicesDisposeSystemSoundID(soundID);

查看更多
欢心
3楼-- · 2019-03-31 20:02

You could simplify things a lot by using system sounds. Look up: AudioServicesCreateSystemSoundID in the documentation. There is also a "System Sound Services Reference Document" that talks about that and other related functions. This is a simple and efficient way to play short sounds. Not sure if it will solve your latency issues but its a good start. You may also try using some different sound file types. Perhaps there is an issue with how it was or was not compressed.

查看更多
神经病院院长
4楼-- · 2019-03-31 20:03

Maybe you could look at AppSoundEngine. It addresses latency and greatly simplifies using of System Sound Services, because it is objective-c wrapper for SystemSoundID and associated C functions.

You can not get acceptable latency (<10 ms) from AVAudioPlayer. System Sound Services is the way to go.

查看更多
三岁会撩人
5楼-- · 2019-03-31 20:11

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
查看更多
一夜七次
6楼-- · 2019-03-31 20:11

One simple fix would just be to do the AVAudioPlayer alloc init for the 2 sounds in your buttonSoundViewController's init method. Then those 2 audio players will already be ready to play in your button delegates.

The fastest way to play sounds is to use the RemoteIO Audio Unit, but that's a far more advanced and complicated looking API.

查看更多
登录 后发表回答