如何淡出 NSSound 对象
我写了一个便宜的& 我在 Mac 上安装了欢快的音板,然后我用 NSSound 播放各种声音,如下所示:
-(void)play:(NSSound *)soundEffect:(BOOL)stopIfPlaying {
BOOL wasPlaying = FALSE;
if([nowPlaying isPlaying]) {
[nowPlaying stop];
wasPlaying = TRUE;
}
if(soundEffect != nowPlaying)
{
[soundEffect play];
nowPlaying = soundEffect;
} else if(soundEffect == nowPlaying && ![nowPlaying isPlaying] && !wasPlaying) {
[nowPlaying play];
}
}
我希望它在几秒钟左右的时间内淡出,而不是让它停止播放。
I've written a cheap & cheerful sound board in for my Mac, and I play the various sounds with NSSound like this:
-(void)play:(NSSound *)soundEffect:(BOOL)stopIfPlaying {
BOOL wasPlaying = FALSE;
if([nowPlaying isPlaying]) {
[nowPlaying stop];
wasPlaying = TRUE;
}
if(soundEffect != nowPlaying)
{
[soundEffect play];
nowPlaying = soundEffect;
} else if(soundEffect == nowPlaying && ![nowPlaying isPlaying] && !wasPlaying) {
[nowPlaying play];
}
}
Rather than just stop it dead, I'd like it to fade out over a couple of seconds or so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 NSTimer 来避免阻塞主线程。
I would use NSTimer to avoid blocking the main thread.
这是该方法的最终版本:
因此,只有当我传入相同的声音(即单击相同的按钮)时,它才会淡出,而且,我选择了 nanosleep 而不是睡眠,因为它的粒度为 1 秒。
我花了一段时间试图弄清楚为什么我的 200 毫秒延迟似乎没有任何效果,但 200 纳秒实际上并没有那么长,是吗:-)
This is the final version of the method:
So it only fades out if I pass the same sound in (ie, click the same button), also, I went for nanosleep rather than sleep, as that on has a granularity of 1 second.
I struggled for a while trying to work out why my 200 millisecond delay didn't seem to have any effect, but then 200 NANOseconds isn't really that long is it :-)
也许是这样的? 您可能想要更线性的下降,但基本思想是创建一个循环并休眠一段时间直到下一次更新。
Something like this perhaps? You probably want a more linear dropoff, but the basic idea is make a loop and sleep the period of time till the next update.