9

我正在使用 SimpleAudioEngine 并且我试图在继续之前检测声音效果是否已完成播放。

我正在寻找任何方法,但我尝试实施的方法不起作用!

CDSoundEngine *engine = [CDAudioManager sharedManager].soundEngine;    
ALuint soundId = [[SimpleAudioEngine sharedEngine] playEffect:soundId];

float seconds = [engine bufferDurationInSeconds:soundId];

每次我使用 bufferDurationInSeconds 时,它都会返回一个浮点值 -1 到变量秒。我检查了实现,当 ID 无效时它返回 -1,但我 100% ID 是有效的!

谁能帮我解决这个问题,或者建议我另一种方法来检测声音效果的结束?

4

1 回答 1

9

Violà! Getting CDSoundSource, and then the soundId from that works.

(The second line is optional, it just plays the sound).

CDSoundEngine *engine = [CDAudioManager sharedManager].soundEngine;
[[SimpleAudioEngine sharedEngine] playEffect:@"soundeffect.m4a"];
CDSoundSource *aSound =[[SimpleAudioEngine sharedEngine] soundSourceForFile:@"soundeffect.m4a"];
float seconds = [engine bufferDurationInSeconds:aSound.soundId];

Also to run a method right when it finishes playing I would use an NSTimer that uses the seconds result.

[NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(aMethod) userInfo:nil repeats:NO];

And then the final method implements something.

-(void)aMethod 
{
NSLog(@"finished playing");
}

Although, this doesn't effect the -1 result, I have to point out that the soundId variable appears twice as two different kinds in your code, in the same line, which will cause problems. No worries though, I have tested my method above with success.

ALuint **soundId** = [[SimpleAudioEngine sharedEngine] playEffect:**soundId**];
于 2013-03-30T09:50:36.167 回答