0

Below is my code to play AVQueueplayer with some web URL, but it takes almost 10-20 seconds to play first song. I have 10 songs, but for reference and to make it small, I have just kept one song here.

    arrSongs = ["http://radiotaj.com/music/1/1836.mp3"]
    let index = 0
    let strSong = (arrSongs.object(at: index)) as? String
    playerItem = AVPlayerItem(url: URL(string:strSong!)!
    let playerItems = [playerItem]
    player = AVQueuePlayer(items : playerItems as! [AVPlayerItem])
    player.play()

Below are the solutions I have already tried

  1. Adding CMTime
  2. Making it pause and play again
  3. It works perfectly fine if file is local.

Any help is greatly appreciated.

Thank you!

Aks
  • 11
  • 1
  • 4
  • http://stackoverflow.com/a/40079765/6656894 refer this answer – Himanshu Moradiya Feb 02 '17 at 09:50
  • Doesn't work. It's AVPlayer. I need it for AVQueuePlayer and that too in SWIFT – Aks Feb 02 '17 at 10:00
  • if you have single url for play audio then i suggest you to use AVPlayer and in multiple urls then use AVQueuePlayer – Himanshu Moradiya Feb 02 '17 at 10:03
  • Yes correct. If you see in my question only I have mentioned, I have 10 songs to play. Just for sample, I have given one. I need to make this AVQueuePlayer work. Your any help would be greatly appreciated. Thanks! – Aks Feb 02 '17 at 10:04
  • then use NSNotificationCenter in your player like [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[theItems lastObject]]; // is theItems your array of url and pass last object in that – Himanshu Moradiya Feb 02 '17 at 10:06
  • Why would it need any Notification? Did you ready my question, it's lagging in playing first song only. Other songs there is no issue. Only first song's lagging is big. – Aks Feb 02 '17 at 10:09
  • You can just try running my code into your xCode and see, it's just 10 lines code and player is AVQueuePlayer. – Aks Feb 02 '17 at 10:10

1 Answers1

0

You can try the following code, it works for me. Here is the version without Alamofire (replace yourURL with the actual link):

override func viewDidLoad() {
    super.viewDidLoad()
    downloadFileFromURL(url: URL(string: yourURL)!)
}

 func play(url: URL) {
    do {
        let songData = try Data(contentsOf: url, options: NSData.ReadingOptions.mappedIfSafe)
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
        player = try AVAudioPlayer(data: songData, fileTypeHint: AVFileTypeAppleM4A)
        player!.prepareToPlay()
        player!.play()
    } catch {
        print(error)
    }
}


func downloadFileFromURL(url: URL) {
    var downloadTask = URLSessionDownloadTask()
    downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: {
        customURL, response, error in

        self.play(url: customURL!)
    })
    downloadTask.resume()
}

Using Alamofire is faster, it basically downloads the file into the Documents folder and plays it from here (don't forget to install Alamofire and type outside the class import Alamofire:

func play(url: URL) {
    do {
        let songData = try Data(contentsOf: url, options: NSData.ReadingOptions.mappedIfSafe)
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
        player = try AVAudioPlayer(data: songData, fileTypeHint: AVFileTypeAppleM4A)
        player!.prepareToPlay()
        player!.play()
    } catch {
        print(error)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        documentsURL.appendPathComponent("song."+"mp3")
        return (documentsURL, [.removePreviousFile])
    }

    Alamofire.download(yourURL, to: destination).response { response in
        if response.destinationURL != nil {
            songURL = response.destinationURL!
            self.play(url: songURL!)
        }
    }
}
Toma
  • 2,764
  • 4
  • 25
  • 44