如何检测音效何时播放完毕?

2024-08-12移动开发问题
1

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

问题描述

我正在使用 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**];

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

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21