【问题标题】:How to detect when sound effect finish playing?如何检测音效何时播放完毕?
【发布时间】:2013-03-12 22:50:55
【问题描述】:

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

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

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

float seconds = [engine bufferDurationInSeconds:soundId];

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

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

【问题讨论】:

  • This 可能会引导您朝着正确的方向前进。

标签: ios objective-c audio cocos2d-iphone simpleaudioengine


【解决方案1】:

维奥拉!获取 CDSoundSource,然后从中获取 soundId。

(第二行是可选的,它只是播放声音)。

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

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

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

然后最后的方法实现了一些东西。

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

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

ALuint **soundId** = [[SimpleAudioEngine sharedEngine] playEffect:**soundId**];

【讨论】:

  • 太棒了!我要疯了,试图找到这个问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 2011-06-04
相关资源
最近更新 更多