swiftaudioavfoundationavplayeravplayeritem

AVPlayer not playing despite having correct URL


I need help from my code since I can't figure out why my AVPlayer can't play the audio. Here would be my function:

@IBAction func pausePlayAudio(_ sender: CustomButton) {
    let audioSourceURL: String = "[Site]/module_lesson_uploads/audios/"
    let audioString: String = audioSourceURL + (sender.paramaters["thisAudioURL"] as! String)
    let audioURL = URL(string: audioString)

    print(audioString)

    let playerItem: AVPlayerItem = AVPlayerItem(url: audioURL!)
    let audioPlayer = AVPlayer(playerItem: playerItem)

    audioPlayer.play()
}

it is called here:

pausePlayButton.addTarget(self, action: #selector(ModuleLessonElementsViewController.pausePlayAudio(_:)), for: .touchUpInside)
pausePlayButton.paramaters["thisAudioURL"] = content

and here would be the console's result from printing the passed String:

[Site]/module_lesson_uploads/audios/228F0E3-1576580339.mp3
2019-12-19 11:54:34.524954+0800 AppUI[3807:70260] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:54:34.688244+0800 AppUI[3807:70259] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:54:34.718316+0800 AppUI[3807:70261] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:54:34.912438+0800 AppUI[3807:70259] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:54:34.913975+0800 AppUI[3807:70259] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
[Site]/module_lesson_uploads/audios/4AB4136-1576647127.mp3
2019-12-19 11:55:04.511064+0800 AppUI[3807:70259] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:55:04.742723+0800 AppUI[3807:70262] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:55:04.795568+0800 AppUI[3807:70262] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:55:05.042987+0800 AppUI[3807:70262] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
2019-12-19 11:55:05.049440+0800 AppUI[3807:70262] [] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection

It seems to be working, but the audio was not playing. Any help or explanation would be greatly appreciated!


Solution

  • I have checked your code and discovered such a pattern:

    Please, check this:

    var audioPlayer = AVPlayer()
    
    @IBAction func pausePlayAudio(_ sender: CustomButton) {
        let audioSourceURL: String = "[Site]/module_lesson_uploads/audios/"
        let audioString: String = audioSourceURL + (sender.paramaters["thisAudioURL"] as! String)
        let audioURL = URL(string: audioString)
    
        print(audioString)
    
        let playerItem = AVPlayerItem(url: audioURL!)
        self.audioPlayer = AVPlayer(playerItem: playerItem)
    
        self.audioPlayer.play()
    }