ios - AVPlayer 和 AVAudioPlayer 不从 URL 播放音频
<p><p>我试图从 URL 播放音频。我尝试了 AVPlayer 和 AVAudioPlayer 但它们都不起作用。</p>
<p>这里是 <a href="http://www.noiseaddicts.com/samples_1w72b820/2514.mp3" rel="noreferrer noopener nofollow">URL</a>我正在尝试播放。</p>
<p>这是我正在使用的代码。</p>
<pre><code> // AVPlayer Code
self.avPlayerItem = AVPlayerItem.init(url: urlOfAudio)
self.avPlayer = AVPlayer.init(playerItem: avPlayerItem)
self.avPlayer.volume = 1.0
self.avPlayer.play()
// AVAudioPlayer
if let url = URL.init(string: escapeCharactorString){
do {
self.playerReference = try AVAudioPlayer.init(contentsOf: url)
guard let player = playerReference else {
return
}
player.volume = 1.0
player.prepareToPlay()
player.numberOfLoops = -1
player.play()
} catch let error {
}
}
</code></pre>
<p>它们都不适合我。
请让我知道我哪里出错了。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的代码没有任何问题,它很完美,但问题与音频文件存储的服务器设置有关。这是您成功播放另一个 mp3 文件的工作代码,</p>
<pre><code>var avPlayer:AVPlayer?
var avPlayerItem:AVPlayerItem?
override func viewDidLoad() {
super.viewDidLoad()
// let urlstring = "http://www.noiseaddicts.com/samples_1w72b820/2514.mp3"
let urlstring = "https://s3.amazonaws.com/kargopolov/kukushka.mp3"
let url = NSURL(string: urlstring)
print("playing \(String(describing: url))")
avPlayerItem = AVPlayerItem.init(url: url! as URL)
avPlayer = AVPlayer.init(playerItem: avPlayerItem)
avPlayer?.volume = 1.0
avPlayer?.play()
}
</code></pre>
<p>所以请尝试使用不同主机的 mp3 文件,它会解决您的问题</p></p>
<p style="font-size: 20px;">关于ios - AVPlayer 和 AVAudioPlayer 不从 URL 播放音频,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46175589/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46175589/
</a>
</p>
页:
[1]