简体   繁体   English

Swift:当歌曲结束时重播AVAudioPlayer

[英]Swift : Replay AVAudioPlayer when song ends

Im trying to get the song to replay when the song ends but it doesn't seem to work. 我想在歌曲结束时让歌曲重播,但似乎不起作用。 this is how I'm implementing it: 这就是我实现它的方式:

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

Any idea how i can get the song to constantly play in a loop 任何想法我怎么能让这首歌不断循环播放

Try using numberOfLoops : 尝试使用numberOfLoops

Swift: 迅速:

var numberOfLoops: Int

OBJECTIVE-C Objective-C的

@property NSInteger numberOfLoops

Setting it to -1 will loop indefinitely... 将其设置为-1将无限循环...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM