[英]How to add url in avaudioplayer in swift
i just want to ask on how to play audio using avAudion using url link.. 我只是想问一下如何使用URL链接使用avAudion播放音频。
and im getting error bad request.. can someone help me? 和即时通讯收到错误的错误请求..有人可以帮助我吗?
i already tried using avplayer but it is not suitable for me.. though it is working but.. i kinda prefer to use avaudioplayer 我已经尝试过使用avplayer,但是它不适合我。.尽管它可以工作,但是..我还是比较喜欢使用avaudioplayer
let mp3URL = NSURL(fileURLWithPath:"https://s3.amazonaws.com/kargopolov/kukushka.mp3")
do {
// 2
audioPlayer = try AVAudioPlayer(contentsOf: mp3URL as URL)
audioPlayer.play()
// 3
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateAudioProgressView), userInfo: nil, repeats: true)
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: false)
}
catch {
print("An error occurred while trying to extract audio file")
}
According to the AVAudioPlayer documentation , if you want to play audio over the internet (that's not already downloaded to disk or memory) you should use AVPlayer . 根据AVAudioPlayer文档 ,如果要通过Internet播放音频(尚未下载到磁盘或内存),则应使用AVPlayer 。
The docs describe AVAudioPlayer as 该文档将AVAudioPlayer描述为
An audio player that provides playback of audio data from a file or memory. 音频播放器,可播放文件或内存中的音频数据。
A little further down, in the overview section, emphasis added: 再往下一点,在概述部分中,重点增加了:
Use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency. 除非要播放从网络流捕获的音频或需要非常低的I / O延迟, 否则请使用此类进行音频播放。
catch {
to catch let error {
and then you can log out the error as part of your message, like so: 更改catch {
以catch let error {
,然后您可以将错误作为消息的一部分注销,如下所示: catch let error{
print("An error occurred while trying to extract audio file: \(error)")
}
An error occurred while trying to extract audio file: Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)" 尝试提取音频文件时发生错误:Error Domain = NSOSStatusErrorDomain Code = 2003334207“(null)”
Domain
and Code
. 注意错误的Domain
和Code
。 Pasting that error into Google yields some results that indicate that the URL might not be resolving correctly. 将该错误粘贴到Google会产生一些结果,表明该URL可能无法正确解析。 (Indeed, clicking on that mp3 link shows a bad access error message.) (实际上,单击该mp3链接将显示错误的访问错误消息。) The first result is another StackOverflow post with a similar issues. 第一个结果是另一个StackOverflow帖子 ,出现类似问题。 The second one is an Apple Developer Forum post which has some more information. 第二篇是苹果开发者论坛的文章 ,其中包含更多信息。
Let's try to change the URL to a publicly accessible sample mp3 file. 让我们尝试将URL更改为可公开访问的示例mp3文件。 (I found this one by searching the web for "test mp3 file" on Google.) (我是通过在网络上搜索Google上的“测试mp3文件”来找到的。)
You'll want to change NSURL
to URL
, and instead of fileURLWithPath
, you're going to want to use another initializer. 您将需要将NSURL
更改为URL
,而不是fileURLWithPath
,您将要使用另一个初始化器。 (Say, string:
.) (说, string:
。)
Whenever you see contentsOf...:
in a media or file API, there's a good chance it expects data or a file, to the exclusion of a network stream. 每当您在媒体或文件API中看到contentsOf...:
时,很有可能期望数据或文件被排除在网络流之外。 Similarly, when you see an initializer or method that takes a fileURL...
, the system expects to be pointing to a local resource, not a network URL. 同样,当您看到带有fileURL...
的初始化程序或方法时,系统期望指向的是本地资源,而不是网络URL。
if let mp3URL = URL(string: "https://s3.amazonaws.com/kargopolov/kukushka.mp3"){
// do s.t
}
Anyway, you should know the basics of ios 无论如何,您应该了解ios的基础知识
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.