如何在后台线程上淡出 AVAudioPlayer?
我有一个音频文件,当用户滚动 UIScrollView 时需要淡出。但是,任何 performSelector:withObject:afterDelay:
方法都会被阻止,直到用户停止滚动。因此,我尝试创建一些代码来在另一个线程上执行淡出:
- (void)fadeOut
{
[NSThread detachNewThreadSelector:@selector(fadeOutInBackground:) toTarget:self withObject:self.audioPlayer];
}
- (void)fadeOutInBackground:(AVAudioPlayer *)aPlayer
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
[self performSelector:@selector(fadeVolumeDown:) withObject:aPlayer afterDelay:0.1];
[myPool release];
}
- (void)fadeVolumeDown:(AVAudioPlayer *)aPlayer
{
aPlayer.volume = aPlayer.volume - 0.1;
if (aPlayer.volume < 0.1) {
[aPlayer stop];
} else {
[self performSelector:@selector(fadeVolumeDown:) withObject:aPlayer afterDelay:0.1];
}
}
它到达了 PerformSelector,但没有进一步,因为我猜它正在尝试在它无法访问的线程上执行。我什至无法更改performSelector:onThread:withObject:waitUntilDone:,因为没有延迟选项。
有什么想法吗?为什么他们让淡出声音变得如此困难? 抱怨
谢谢!
I have an audio file which needs to fade out while the user is scrolling a UIScrollView. However, any performSelector:withObject:afterDelay:
method is blocked until the user has stopped scrolling. So I have tried to create some code to perform a fadeout on another thread:
- (void)fadeOut
{
[NSThread detachNewThreadSelector:@selector(fadeOutInBackground:) toTarget:self withObject:self.audioPlayer];
}
- (void)fadeOutInBackground:(AVAudioPlayer *)aPlayer
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
[self performSelector:@selector(fadeVolumeDown:) withObject:aPlayer afterDelay:0.1];
[myPool release];
}
- (void)fadeVolumeDown:(AVAudioPlayer *)aPlayer
{
aPlayer.volume = aPlayer.volume - 0.1;
if (aPlayer.volume < 0.1) {
[aPlayer stop];
} else {
[self performSelector:@selector(fadeVolumeDown:) withObject:aPlayer afterDelay:0.1];
}
}
It gets as far as the performSelector, but no further because I guess it's trying to perform on a thread it has no access to. I can't even change it for performSelector:onThread:withObject:waitUntilDone:
because there is no delay option.
Any ideas? Why have they made it so hard to just fade out a sound? moan
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过以不同于默认模式的运行循环模式安排选择器解决了类似的问题。这样它就不会干扰滚动事件。使用
NSRunLoopCommonModes
对我有用:I resolved a similar issue by scheduling the selector in a different run loop mode than the default one. This way it is not interfering with the scrolling events. Using the
NSRunLoopCommonModes
worked for me:受到上面答案的启发
Inspired by the answer above