AvAudioPlayer无法正常工作? [英] AvAudioPlayer not working?

查看:62
本文介绍了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无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆