Swift : 歌曲结束时重播 AVAudioPlayer

Swift : Replay AVAudioPlayer when song ends

我试图让这首歌在歌曲结束时重播,但它似乎不起作用。这就是我的实现方式:

var music1 : AVAudioPlayer! = AVAudioPlayer()


func setUpMusic(){

    let music1Path = NSBundle.mainBundle().pathForResource("Lost", ofType:"mp3")


    let fileURL1 = NSURL(fileURLWithPath: music1Path!)

    do{

        music1 = try AVAudioPlayer(contentsOfURL: fileURL1, fileTypeHint: nil)

    }

    catch{}

    music1.play()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameScene.replayFirstSong(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: music1)

}


func replayFirstSong(note: NSNotification){
    print("Inside Replay first song")
    music1.currentTime = 0
    music1.play()
}

知道如何让歌曲不断循环播放吗

尝试使用 numberOfLoops:

Swift:

var numberOfLoops: Int

OBJECTIVE-C

@property NSInteger numberOfLoops

设置为-1会无限循环...

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#//apple_ref/occ/instp/AVAudioPlayer/numberOfLoops