Audio player is uninitialized Swift

I’m trying to "touch" to the audio player in KikiAudioPlayer. Here is my view from where I want to do that.

class AudioBarView: UIView {      @IBOutlet weak var playPauseButton: UIButton!     @IBOutlet weak var nowPlayingSongLabel: UILabel!          var kikiAudioPlayer = KikiAudioPlayer()          @IBAction func playPauseButtonPressed(_ sender: UIButton) {         let playImage = UIImage(systemName: "play.fill")         if playPauseButton.currentImage == playImage {             playPauseButton.setImage(UIImage(systemName: "pause.fill"), for: .normal)             kikiAudioPlayer.play()         } else {             playPauseButton.setImage(UIImage(systemName: "play.fill"), for: .normal)             kikiAudioPlayer.pause()         }     } } 

And here’s the snippet of "KikiAudioPlayer" and declaration of the audio player.

class KikiAudioPlayer: NSObject, AVAudioPlayerDelegate {      var audioPlayer = AVAudioPlayer()      func play() {         audioPlayer.play()         delegate?.audioPlayerPlayingStatusChanged(isPlaying: true)         updater = CADisplayLink(target: self, selector: #selector(updateDelegate))         updater.add(to: RunLoop.current, forMode: RunLoop.Mode.common)         setWaiting(to: false)     }      func pause() {         audioPlayer.pause()         delegate?.audioPlayerPlayingStatusChanged(isPlaying: false)         updater.invalidate()         setWaiting(to: true)     }  } 

When the button pressed my breakpoint said that. It means that I press the button nothing changes.

po kikiAudioPlayer.audioPlayer <uninitialized> 

enter image description here

The question is why the audio player in AudioBarView is uninitialized if I created an instance of KikiAudioPlayer.

I didn’t provide any asset to the player(it’s not important) because the audio player is working fine but the problem is that can’t pause and play it.

I’ve tried to clean the build folder but it didn’t help.

Add Comment
2 Answer(s)

I would argue with this line of code:

var audioPlayer = AVAudioPlayer() 

AVAudioPlayer doesn’t have init() listed as a valid initializer. Even though you can use it without compliler warning. The official document shows four separate initializers, all of which take at least one parameter.

I sugguest you checking out https://www.onooks.com/psa-avaudioplayer-alloc-init-crashes-in-ios-13-1-and-avaudioplayer/ this blog for more information.

Updated: https://developer.apple.com/documentation/avfoundation/avaudioplayer

Answered on September 1, 2020.
Add Comment

The answer was simpler than I think. I deleted the declaration of the audio player in KikiAudioPlayer and added it in the App delegate.

var audioPlayer: AVAudioPlayer! 
Answered on September 1, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.