“Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type”

时光总嘲笑我的痴心妄想 提交于 2019-12-14 03:04:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!