swift : Replay AVAudioPlayer when song ends

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

知道如何让这首歌不断循环播放

最佳答案

尝试使用numberOfLoops:

swift :

var numberOfLoops: Int

目标-C

@property NSInteger numberOfLoops

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

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

关于 swift : Replay AVAudioPlayer when song ends,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36491027/

相关文章:

iphone - AudioServicesPlaySystemSound 和 AVAudioPlayer 可以混用吗?

iphone - 路由 iPhone 音频声音

ios - 增加 NavigationBar 高度

objective-c - inputAccessoryView 关闭键盘

swift - 无法在 View Controller 之间传递数据

swift - 在 App Delegate (tvOS) 中引用 AVAudioPlayer

ios - 为什么我的多 channel 映射不能正常工作?

ios - Swift - AVAudioPlayer 不播放声音

ios - 访问不同 Controller 中的功能并初始化我的 masterViewController?

ios - swiftUI中scrollView的填充高度