3
當歌曲結束時,我試圖讓歌曲重播,但它似乎不起作用。這是我如何實現它: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()
}
任何想法如何,我可以得到這首歌在循環中不斷髮揮
得到它!比方說,我希望它是無限的,但如果我希望它停止時,用戶做了什麼,我可以讓它停止。現在我做music1.stop()但不起作用,它只是播放 – OriginalAlchemist
@OriginalAlchemist:使用music1.pause()&for repeat player.repeatMode = MPMovieRepeatMode.init(rawValue:5)! –
你需要一個IBAction(比如一個按鈕)或者另一個函數來調用stop() - [閱讀關於使用它的說法](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/ AVAudioPlayerClassReference /#// apple_ref/occ/instm/AVAudioPlayer/stop)與currentTime(你可能想要擺脫) –