尽管有正确的 URL,但 AVPlayer 没有播放
AVPlayer not playing despite having correct URL
我需要代码的帮助,因为我无法弄清楚为什么我的 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
这里是控制台打印传递的字符串的结果:
[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
它似乎工作正常,但没有播放音频。任何帮助或解释将不胜感激!
我检查了你的代码并发现了这样一个模式:
- 如果我在
viewDidLoad
之前定义并初始化 audioPlayer
- 那么 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()
}
您的错误在于您正在为音频播放器创建功能范围实例。它会在执行后立即释放,这就是它不播放的原因。
为 audioPlayer 对象创建一个 class 变量并执行相同的操作。它肯定会起作用。此外,目前您的 URL 来自服务器,因此请等待几秒钟以接收足够的缓冲区数据来播放。
我需要代码的帮助,因为我无法弄清楚为什么我的 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
这里是控制台打印传递的字符串的结果:
[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
它似乎工作正常,但没有播放音频。任何帮助或解释将不胜感激!
我检查了你的代码并发现了这样一个模式:
- 如果我在
viewDidLoad
之前定义并初始化audioPlayer
- 那么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()
}
您的错误在于您正在为音频播放器创建功能范围实例。它会在执行后立即释放,这就是它不播放的原因。 为 audioPlayer 对象创建一个 class 变量并执行相同的操作。它肯定会起作用。此外,目前您的 URL 来自服务器,因此请等待几秒钟以接收足够的缓冲区数据来播放。