我试图为Swift 3中的每个视图控制器提供不同的背景音乐

我试图为Swift 3中的每个视图控制器提供不同的背景音乐,swift,Swift,试图使视图控制器具有不同数量的循环,但音乐在所有视图控制器中仅播放一次 音乐助手代码: import AVFoundation class MusicHelper1 { static let sharedHelper = MusicHelper1() func playBackgroundMusic(fileName: String, withExtenstion fileExtension: String,withExtenstion numberOfLoops:Int) {

试图使视图控制器具有不同数量的循环,但音乐在所有视图控制器中仅播放一次

音乐助手代码:

import AVFoundation

class MusicHelper1 {
static let sharedHelper = MusicHelper1()


func playBackgroundMusic(fileName: String, withExtenstion fileExtension: String,withExtenstion numberOfLoops:Int) {

    do {
        AVPlayer1 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: fileName, ofType: fileExtension)!))

        AVPlayer1.prepareToPlay()

        AVPlayer1.play()

    } catch {
        print(error)
    }

}

func stopBackgroundMusic(fileName: String, withExtenstion fileExtension: String) {


        AVPlayer1.stop()

}
}

和我的第二视图控制器代码:

进口基础 导入UIKit 进口AVF基金会 类别编号:UIViewController{

let soundFilenames = ["1","2","3","4","5","6","7","8","9","10" ]
var audioPlayers = [AVAudioPlayer]()
override func viewDidLoad() {
    super.viewDidLoad()




    for sound in soundFilenames {
        do {
            let audioPath = Bundle.main.path(forResource: sound, ofType: "wav")
            let audioPlayer = try AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)

            audioPlayers.append(audioPlayer)

        }

        catch
        {

            audioPlayers.append(AVAudioPlayer())

        }
    }

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

@IBAction func didTap(_ sender: UIButton) {
    let tag = sender.tag
    let audioPlayer = audioPlayers[tag]
    audioPlayer.play()
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    MusicHelper1.sharedHelper.playBackgroundMusic(fileName: "number", withExtenstion: "wav", withExtenstion: -1)


        }
}


我该怎么办?

在当前编写的过程中,它看起来像是一个可变范围的问题。您的
视图将出现()
首先访问全局单例
MusicHelper1.sharedHelper
调用
.stopBackgroundMusic()
,这很好。但是,您可以使用以下方法创建新的AVAudioPlayer:

var AVPlayer1 = AVAudioPlayer()
因此,
AVPlayer1
是一个局部变量,其作用域在
viewwillappease()方法中。当
viewwillbeen()
结束时,该变量超出范围。由于没有更多的引用,它很快被垃圾收集,音乐停止

由于您的
MusicHelper1
类中已经有了
playBackgroundMusic
函数,我建议您直接调用它


另外,切题地说,我建议将您用于创建音频播放器列表的代码从
viewwillbeen()
移动到
viewDidLoad()
,这样只会发生一次(当视图首次加载时,而不是每次出现时)。

嗨,CJ Hutchison…感谢您的回复……不过我只使用Swift 3,不是雪碧套装。在Swift 3中有没有办法做到这一点?仍在等待回复…我的项目因此而推迟。谢谢你的回复,罗伯特。我试过你建议的密码。当我按下按钮时,第一首歌停止了,但在视图实际出现之前,我的新歌停止了。我该怎么办?@kunalkapor你的歌有多长?新的视图控制器应该几乎立即出现。如果你发布你正在使用的代码,也许我可以帮你。这首歌不是很长…可能需要1分钟左右…我需要在视图控制器中为背景音乐编码。我的第二个视图控制器中也有按钮,每次按下按钮时都需要播放一个声音,这是很好的导入AVFoundation类MusicHelper1{static let sharedHelper=MusicHelper1()var AVPlayer1=AVAudioPlayer()func playBackgroundMusic(){do{AVPlayer1=try AVAudioPlayer(contentsOf:URL.init(fileURLWithPath:Bundle.main.path(用于资源:“音乐”,类型为:“wav”)!)AVPlayer1.preparetoplayer()AVPlayer1.numberOfLoops=-1 AVPlayer1.prepareToPlay()AVPlayer1.play()}catch{print(error)}func stopBackgroundMusic(){AVPlayer1.stop}}以及在第二视图控制器中