如何检测音效何时播放完毕? [英] How to detect when sound effect finish playing?

查看:22
本文介绍了如何检测音效何时播放完毕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SimpleAudioEngine,并尝试在继续之前检测音效是否播放完毕.

I'm using SimpleAudioEngine and I'm trying to detect if a sound effect is finish playing before continuing.

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

I'm looking for any method, but the one I'm trying to implement doesn't work!

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

float seconds = [engine bufferDurationInSeconds:soundId];

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

Every time I use bufferDurationInSeconds, it returns a float value of -1 to variable seconds. I checked the implementation, and it returns a -1 when the id is not valid, but I'm 100% the ID is valid!

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

Can anyone help me on this problem, or suggest me another way to detect the end of an sound effect?

推荐答案

Violà!获取 CDSoundSource,然后从中获取 soundId.

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];

还要在播放完毕后立即运行一个方法,我会使用一个使用秒结果的 NSTimer.

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");
}

虽然这不会影响 -1 结果,但我必须指出,soundId 变量在您的代码中以两种不同的类型出现两次,在同一行中,这会导致问题.不过不用担心,我已经成功测试了上面的方法.

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**];

这篇关于如何检测音效何时播放完毕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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