如果用户离开视图如何停止音频播放
我有一个音频文件,当用户单击一个视图时会播放该音频文件。我的问题是,如果他们移动到另一个视图,音频会继续播放。如果用户不再处于该视图中,我希望音频停止。
这是在 viewDidUnload 方法中完成的吗?
这是 15 秒延迟后播放音频的代码:
-(void)startPlaying {
[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(startPlaying) userInfo:nil repeats:NO];
NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"];
CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath];
AudioServicesCreateSystemSoundID(audioURL, &audioID);
AudioServicesPlaySystemSound(audioID);
}
感谢您的帮助。
I have an audio file that plays when a user clicks on one view. my problem is that if they move to another view the audio continues to play. I would like the audio to stop if the user is no longer on that view.
is this done in the viewDidUnload method?
here's the code that plays the audio after a 15sec delay:
-(void)startPlaying {
[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(startPlaying) userInfo:nil repeats:NO];
NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"];
CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath];
AudioServicesCreateSystemSoundID(audioURL, &audioID);
AudioServicesPlaySystemSound(audioID);
}
thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-(void)viewWillDisappear:(BOOL)动画{
[超级viewWillDisappear:动画];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}