开发者

Sounds randomly interrupted when starting to play

开发者 https://www.devze.com 2022-12-16 12:11 出处:网络
I\'m calling playSoundFromBundle from the code below to play sounds (aif files).I have a sound that does a single click and then a fading sound.Both sounds are in the same file.Sometimes I get two cli

I'm calling playSoundFromBundle from the code below to play sounds (aif files). I have a sound that does a single click and then a fading sound. Both sounds are in the same file. Sometimes I get two clicks and then the fade. Meaning, click, click...fade. A single click isn't what should play. I'm guessing the sound starts (click sound), gets interrupted and then restarts (full sound...click/fade) because of other processing that may be going on. It seems random when it will occur. I put the sound on its own thread to try and avoid the double clicking. Is there anything else I can do to ensure the sound plays correctly?

- (void) playSoundFromBundleThreaded:(NSArray*)arr{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *fileName = (NSString*)[arr objectAtIndex:0];
NSString *fileExt = (NSString*)[arr objectAtIndex:1];
NSError *err;
AVAudioPlayer *newP开发者_运维问答layer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: fileName ofType: fileExt inDirectory:@"/"]] error: &err];
self.audioPlayer = newPlayer;
self.audioPlayer.numberOfLoops = 0;
self.audioPlayer.volume = .5;

if (self.audioPlayer == nil)
{
    NSLog(@"Problem initializing Sound - %@", [err description]);
}
else
{
    [self.audioPlayer play];
}
[newPlayer release];
[pool release];
}

- (void) playSoundFromBundle:(NSString*)fileName fileExtension:(NSString*)fileExt{
NSArray *arr = [NSArray arrayWithObjects:fileName, fileExt, nil];
[NSThread detachNewThreadSelector: @selector(playSoundFromBundleThreaded:) toTarget:self withObject:arr];
}


No idea if this would work, but check out the Audio Session stuff. It's intended to manage things like whether the playing of other sounds (such as by the music player) will interrupt audio from your application.

0

精彩评论

暂无评论...
验证码 换一张
取 消