AvAudioPlayer无法正常工作?

编程入门 行业动态 更新时间:2024-10-19 21:26:53
本文介绍了AvAudioPlayer无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了AVAudioPlayer对象,并在场景中播放音乐,如果我过渡到另一个场景,然后回到原始场景,它将创建另一个对象,并播放相同的音轨两次.我该如何克服呢?

I create the AVAudioPlayer object and play the music within the scene, if i transition through to another scene and then go back to the original scene it creates another object and plays the same soundtrack twice. How do i overcome this ?

NSURL *url = [[NSBundle mainBundle] URLForResource:@"backgroundMusic" withExtension:@"mp3"]; self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; self.backgroundMusic.numberOfLoops = -1; [self.backgroundMusic prepareToPlay]; if (self.backgroundMusic.playing == NO) { [self.backgroundMusic play]; }

推荐答案

您想做什么? 您是否只想在该场景中播放音乐?好吧,在过渡之前,请停止avaudioplayer.

Well what do you want to do? Do you want to play music only in that scene? Well then before you transition, stop the avaudioplayer.

[audioplayer stop];

或者,也许您想在场景中开始播放音乐,然后让它在离开场景或返回场景时继续播放?

Or, maybe you want to start music in the scene, and let it just continue playing if you leave the scene or go back to the scene?

那么,您将需要一个单独的音乐包装器类.创建一个班级,然后打电话给它播放音乐.您可以检查.playing属性以了解音乐是否已经在播放.

Well then you would need a seperate wrapper class for your music. Create a class, and just put a call to it to play music. You can check the .playing property to know if music is already playing or not.

更多推荐

AvAudioPlayer无法正常工作?

本文发布于:2023-10-26 23:53:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   AvAudioPlayer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!