iOS:滚动视图中的声音

发布于 2024-12-19 22:58:33 字数 555 浏览 2 评论 0原文

我有这样的代码:

- (void) setAudioScroll{
NSString *path = [NSString stringWithFormat:@"%@/%@",
                  [[NSBundle mainBundle] resourcePath],
                  @"scroll1.mp3"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundScroll = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[soundScroll prepareToPlay]; 

}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{


    [soundScroll play];
}

然后我想在滚动视图滚动时发出声音。但是当我滚动它时,我发现它不能正常工作,因为当它调用 mp3 声音时,它会短暂停止滚动。我可以使用单独的线程吗?

I have this code:

- (void) setAudioScroll{
NSString *path = [NSString stringWithFormat:@"%@/%@",
                  [[NSBundle mainBundle] resourcePath],
                  @"scroll1.mp3"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundScroll = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[soundScroll prepareToPlay]; 

}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{


    [soundScroll play];
}

Then I want to make a sound when scrollview scroll. but when I scroll it I see that don't work fine because when it call the mp3 sound it stop its scroll for a short time. Can I use a separated thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花辞树 2024-12-26 22:58:33
- (void) scrollViewDidScroll:(UIScrollView *)scrollView

在一次滚动过程中被多次调用,时间间隔非常短,您不希望每次发生此回调时都调用 [soundScroll play];,而是使用此回调,

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

以便声音滚动开始时播放一次,或者

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

如果您希望在用户滚动后播放声音,则播放其中之一。如果您希望短声音在滚动中连续循环,您可以将 AVAudioPlayer numberOfLoops 属性设置为负值,以便它无限循环,然后在滚动停止时停止它。

- (void) scrollViewDidScroll:(UIScrollView *)scrollView

Is called many times during a single scroll, at very short time intervals, you don't want to be calling [soundScroll play]; every time this callback occurs, instead use this callback

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

so that the sound is played once when the scrolling starts, or either of these

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

if you want the sound the play after the user scrolls. If you want a short sound to be looping continuously through the scroll you can set the AVAudioPlayer numberOfLoops property to a negative value so that it loops indefinately, then stop it when the scrolling stops.

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