我需要代码帮助,因为我无法弄清楚为什么我的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 = AVPlayerItem(url: audioURL!)
let audioPlayer = AVPlayer(playerItem: playerItem)
audioPlayer.play()
}
在这里被称为
pausePlayButton.addTarget(self, action: #selector(ModuleLessonElementsViewController.pausePlayAudio(_:)), for: .touchUpInside)
pausePlayButton.paramaters["thisAudioURL"] = content
,这将是控制台打印传递的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
它似乎正在工作,但是音频没有播放。任何帮助或解释将不胜感激!
答案 0 :(得分:0)
我检查了您的代码并发现了这种模式:
audioPlayer
之前定义和初始化viewDidLoad
-那么audioPlayer
在100%的情况下有效(我使用this音频)audioPlayer
(在pausePlayAudio
)-有时可行,有时不可行。请检查以下内容:
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()
}
答案 1 :(得分:0)
您的错误是您正在为音频播放器创建功能范围实例。它会在执行后立即释放,这就是为什么它不播放。 创建audioPlayer对象的类变量,然后执行相同的操作。它一定会工作。另外,当前您的网址来自服务器,因此请等待几秒钟,以接收足够的缓冲区数据来播放。