[英]Remote mp3 file taking a lot of time to play in swift ios
i'm in trouble. 我有麻烦了。 i want to play a remote mp3 file in my app.
我想在我的应用程序中播放远程mp3文件。 but mp3 file taking a lot of time (approx 5-6 minute) to play.
但是mp3文件要花费很多时间(大约5-6分钟)才能播放。 why ?
为什么呢?
Anyone can suggest what should i do ? 任何人都可以建议我该怎么办?
import UIKit
import AVFoundation
class TestViewController: UIViewController, AVAudioPlayerDelegate {
var player:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func play(sender: AnyObject) {
let url = "http://www.example.com/song.mp3"
let fileURL = NSURL(string: url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()))
let soundData = NSData.dataWithContentsOfURL(fileURL, options: nil, error: nil)
var error: NSError?
self.player = AVAudioPlayer(data: soundData, error: &error)
if player == nil
{
if let e = error
{
println(e.localizedDescription)
}
}
player.volume = 1.0
player.delegate = self
player.prepareToPlay()
player.play()
}
}
Thanks in advance. 提前致谢。
Use AVPlayer instead of AVAudioPlayer for streaming audio. 使用AVPlayer而不是AVAudioPlayer来流音频。 It can be as simple as this --
可以这么简单-
let urlString = "http://www.example.com/song.mp3"
let url = NSURL(string: urlString)
var avPlayer = AVPlayer(URL: url)
avPlayer.play()
You can also set an observer on the AVPlayer.status property to manage its changing status. 您还可以在AVPlayer.status属性上设置观察者以管理其更改状态。 Check out: https://stackoverflow.com/a/13156942/2484290 (objective c)
检出: https : //stackoverflow.com/a/13156942/2484290 (目标c)
And of course the AVPlayer docs are here: https://developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/cl/AVPlayer 当然,AVPlayer文档在这里: https : //developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/cl/AVPlayer
Try using my example where you can work with the cache and the remote file. 尝试使用我的示例来处理缓存和远程文件。 player.automaticallyWaitsToMinimizeStalling = false // by default true, so It is important to solve your problem in a mp3 file remotely
player.automaticallyWaitsToMinimizeStalling = false //默认情况下为true,因此远程解决mp3文件中的问题很重要
This feature is available iOS 10.0+ 此功能适用于iOS 10.0+
var player: AVPlayer!
enum AudioType: String {
case remote
case cache
}
@IBAction func remotePressed(_ sender: Any) {
playAudio(type: .remote, fileURL: "http://www.example.com/song.mp3")
}
@IBAction func cachePressed(_ sender: Any) {
if let fileURL = Bundle.main.path(forResource: "jordan", ofType: "mp3") {
playAudio(type: .cache, fileURL: fileURL)
}
}
private func playAudio(type: AudioType, fileURL: String) {
let url = type == .cache ? URL.init(fileURLWithPath: fileURL) : URL.init(string: fileURL)
let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false//It's important
player.play()
}
Its may be late. 可能已经晚了。 Yes.
是。 what @lemon lime pomelo has said.
@柠檬石灰柚子说了什么。 you should use AVPlayer instead of AVAudioPlayer.
您应该使用AVPlayer而不是AVAudioPlayer。
Also you can use MediaPlayer. 您也可以使用MediaPlayer。
import UIKit
import MediaPlayer
@IBAction func playSong(sender: AnyObject) {
print("Song")
self.playAudio("yourAudioURL.mp3")
}
func playAudio(URL:String){
let movieurl:NSURL? = NSURL(string: "\(URL)")
if movieurl != nil {
self.movie = MPMoviePlayerViewController(contentURL: movieurl!)
}
if self.movie != nil {
self.presentViewController(self.movie!, animated: true, completion: nil)
self.movie?.moviePlayer.play()
}
}
这很慢,因为您的程序会在开始播放之前先下载所有歌曲
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.