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

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

问题描述

我使用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?

推荐答案

中提琴!越来越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变量作为两种不同在code,在同一行,这将导致问题出现两次。不用担心,虽然,我过我的方法上面的成功。

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天全站免登陆