如何在后台线程上淡出 AVAudioPlayer?

发布于 2024-10-02 22:31:47 字数 998 浏览 8 评论 0原文

我有一个音频文件,当用户滚动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦亦如幻 2024-10-09 22:31:47

我通过以不同于默认模式的运行循环模式安排选择器解决了类似的问题。这样它就不会干扰滚动事件。使用 NSRunLoopCommonModes 对我有用:

[self performSelector:@selector(fadeVolumeDown:) 
           withObject:aPlayer
           afterDelay:0.1 
              inModes:[NSArray arrayWithObject: 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:

[self performSelector:@selector(fadeVolumeDown:) 
           withObject:aPlayer
           afterDelay:0.1 
              inModes:[NSArray arrayWithObject: NSRunLoopCommonModes]];
披肩女神 2024-10-09 22:31:47

受到上面答案的启发

   while (theAudio.volume > 0.000000)
        [self fadeVolumeDown];

- (void) fadeVolumeDown{
    theAudio.volume -=0.01;
}

Inspired by the answer above

   while (theAudio.volume > 0.000000)
        [self fadeVolumeDown];

- (void) fadeVolumeDown{
    theAudio.volume -=0.01;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文