问题
I'm having trouble with the function audioPlayerDidFinish playing. I have added the AVAudioPlayerDelegate to my view controller class and defined the function. When I call the function I get the error "Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type" What am I doing wrong? Let me know if I need to add more code for you to see.
func buttonPressed() {
if currentSound == sound {
currentSound = "None"
activateBlankCircle()
audioPlayer.stop()
}
else {
currentSound = sound
audioPlayer.prepareToPlay()
activateRedCircle()
playSound()
audioPlayerDidFinishPlaying(AVAudioPlayer, successfully: Bool)
//Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type
}
}
回答1:
Delegate methods should not be called by the delegate itself.
audioPlayerDidFinishPlaying here is a delegate method. You are supposed to implement it (which you have) and set self as the audio player's delegate. The method will be called by the AVAudioPlayer at an appropriate time i.e. when it finishes playing a sound.
So you should remove the call to audioPlayerDidFinishPlaying and everything should work fine. From the looks for it, you are implementing a start/stop playing button. If you want to do something when the player finishes playing, you can set self as the audioPlayer.delegate in viewDidLoad. And the method will be called when it finishes playing a sound.
来源:https://stackoverflow.com/questions/48201515/cannot-convert-value-of-type-avaudioplayer-type-to-expected-argument-type