简体   繁体   中英

AVPlayer: Playing a “on the fly” Transcode Audio file in Swift

I am working on a project that implements the OpenHome library in swift. I am working on the Renderer provider side which mean that Proxies send media item URLs within the same network for playing. The app after getting a url needs to set the AVPlayer and play.

The URLs provided are like:

If the URL contains "orig" means that the file matches with the extension. On the other side, If the URL contains "trans" means that the file provided will match the extension but it will be served on the fly as it is being converted.

This technique works with Sonos, Android and Chrome browsers when I enter the URL. However it does not work with neither iOS and Safari (Considering I am doing it right).

I am attaching a test ViewController that handles the process

import UIKit
import AVFoundation

class ViewController: UIViewController {

    private var avAudioPlayer:AVPlayer!
    private var avAudioSession:AVAudioSession = AVAudioSession.sharedInstance()

    override func viewDidLoad() {
        super.viewDidLoad()

        try! avAudioSession.setCategory(.playback, mode: .default, options: [])
        try! avAudioSession.setActive(true)

        let urlString = "http://192.168.1.123:8088/ext/trans/audio/2249.mp3"
        let url = URL(string: urlString)!
        avAudioPlayer = AVPlayer(url: url)


        avAudioPlayer?.addObserver(self, forKeyPath: "status", options: [], context: nil)

    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

        if keyPath == "status"{

            switch avAudioPlayer.status{
            case .readyToPlay:
                print("Hey: Ready To Play")
                avAudioPlayer.play()
            case .failed:
                print("FAILED OH Nooo!")
            case .unknown:
                print("Something went wrong but ...")
            }

        }

    }

}
  • NSAllowsArbitraryLoads is set to true

I attach some status data:

print("A Reason = \(String(describing: avAudioPlayer.reasonForWaitingToPlay?.rawValue))")
-->A Reason = "AVPlayerWaitingWhileEvaluatingBufferingRateReason"

print("TIME Control Status = \(avAudioPlayer.timeControlStatus.rawValue)")
-->TIME Control Status = 1

Thank you.

Turns out that the provider side was not compatible with Apple media services. AVPlayer will actually handle it by it self.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM