远程mp3文件需要花费大量时间才能在Swift iOS中播放 [英] Remote mp3 file taking a lot of time to play in swift ios

查看:80
本文介绍了远程mp3文件需要花费大量时间才能在Swift iOS中播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦了.我想在我的应用程序中播放远程mp3文件.但是mp3文件要花费很多时间(大约5-6分钟)才能播放.为什么 ?

i'm in trouble. i want to play a remote mp3 file in my app. but mp3 file taking a lot of time (approx 5-6 minute) to play. 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()

 }

}

谢谢.

推荐答案

使用AVPlayer代替AVAudioPlayer进行音频流传输.可以这么简单-

Use AVPlayer instead of AVAudioPlayer for streaming audio. 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()

还可以在AVPlayer.status属性上设置观察者以管理其更改状态.查看: https://stackoverflow.com/a/13156942/2484290 (目标c)

You can also set an observer on the AVPlayer.status property to manage its changing status. Check out: https://stackoverflow.com/a/13156942/2484290 (objective c)

当然,AVPlayer文档在这里: https://developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/cl/AVPlayer

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

这篇关于远程mp3文件需要花费大量时间才能在Swift iOS中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆