出现audioPlayerDidFinishPlaying
无法调用的问题...
我看过其他一些SO线程:
Swift: AudioPlayerDidFinished will not be called
AVAudioPlayerDelegate doesn't call the method
...但是仍然有相同的问题。
我将我的ViewController
设置为符合AVAudioPlayerDelegate
:
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioEngine = AVAudioEngine()
...
}
并在此处创建播放器;在创建播放器本身之后立即设置代表:
func startAudioEngine(audioFileURL: URL) {
var player: AVAudioPlayer?
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: audioFileURL, fileTypeHint: AVFileType.mp3.rawValue)
guard let player = player else { return }
player.delegate = self
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
}
您的问题是双重的: