【问题标题】:Remote mp3 file taking a lot of time to play in swift ios在 swift ios 中播放远程 mp3 文件需要大量时间
【发布时间】:2018-11-13 21:13:30
【问题描述】:

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

任何人都可以建议我该怎么做?

 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()

 }

}

提前致谢。

【问题讨论】:

    标签: ios swift avfoundation


    【解决方案1】:

    使用 AVPlayer 而不是 AVAudioPlayer 流式传输音频。可以这么简单--

    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)

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

    【讨论】:

      【解决方案2】:

      尝试使用我的示例,您可以在其中使用缓存和远程文件。 player.automaticallyWaitsToMinimizeStalling = false //默认为true,所以远程解决mp3文件中的问题很重要

      此功能适用于 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()
      }
      

      【讨论】:

        【解决方案3】:

        可能晚了。 是的。 @lemon 青柠柚说了什么。你应该使用 AVPlayer 而不是 AVAudioPlayer。

        您也可以使用 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()
            }
        }
        

        【讨论】:

          【解决方案4】:

          这很慢,因为您的程序在开始播放之前下载了所有歌曲

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-11-12
            • 1970-01-01
            • 2011-06-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多