iOS:滚动视图中的声音
我有这样的代码:
- (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在一次滚动过程中被多次调用,时间间隔非常短,您不希望每次发生此回调时都调用
[soundScroll play];
,而是使用此回调,以便声音滚动开始时播放一次,或者
如果您希望在用户滚动后播放声音,则播放其中之一。如果您希望短声音在滚动中连续循环,您可以将
AVAudioPlayer
numberOfLoops 属性设置为负值,以便它无限循环,然后在滚动停止时停止它。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 callbackso that the sound is played once when the scrolling starts, or either of these
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.