Swift:当歌曲结束时重播AVAudioPlayer

时间:2016-04-08 03:30:53

标签: swift avaudioplayer

我想在歌曲结束时让歌曲重播,但它似乎无法奏效。这就是我实施它的方式:

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()
}

任何想法我怎么能让这首歌不断循环播放

1 个答案:

答案 0 :(得分:1)

尝试使用numberOfLoops

夫特:

var numberOfLoops: Int

目标-C

@property NSInteger numberOfLoops

将其设置为-1将无限循环...

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