我有一个音频文件需要在用户滚动 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:
更改它,因为没有延迟选项。
有什么想法吗?为什么他们让淡出声音变得如此困难? 呻吟
谢谢!
最佳答案
我通过将选择器安排在与默认模式不同的运行循环模式中解决了类似的问题。这样它就不会干扰滚动事件。使用 NSRunLoopCommonModes
对我有用:
[self performSelector:@selector(fadeVolumeDown:)
withObject:aPlayer
afterDelay:0.1
inModes:[NSArray arrayWithObject: NSRunLoopCommonModes]];
关于objective-c - 如何淡出后台线程上的 AVAudioPlayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4195056/