播放直播的TuneIn Radio URL iOS Swift [英] Playing a live TuneIn Radio URL iOS Swift

查看:224
本文介绍了播放直播的TuneIn Radio URL iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个旨在通过使用TuneIn URL播放生活广播的应用程序.在TuneIn中,API中有一个http get请求,该请求为JSON提供了所有URL及其比特率.

I am working on an app which is intended to play life radio by using TuneIn URLs. In TuneIn, there is an http get request in the API which provides a JSON with all the URL and its bitrates.

所以 http://opml.radiotime.com/Tune.ashx?id = s150147& formats = aac,mp3& render = json

将返回

{ "head": { "status": "200"}, "body": [
 { "element" : "audio", 
"url": "http://player.absoluteradio.co.uk/tunein.php?i=a664.aac",
"reliability": 95,
"bitrate": 64,
"media_type": "aac",
"position": 0,
"player_width": 529,
"player_height": 716,
"guide_id": "e89279187",
"is_direct": false }, { "element" : "audio", 
"url": "http://player.absoluteradio.co.uk/tunein.php?i=a6low.mp3",
"reliability": 78,
"bitrate": 48,
"media_type": "mp3",
"position": 0,
"player_width": 529,
"player_height": 716,
"guide_id": "e89279188",
"is_direct": false }, { "element" : "audio", 
"url": "http://player.absoluteradio.co.uk/tunein.php?i=a624.aac",
"reliability": 29,
"bitrate": 24,
"media_type": "aac",
"position": 0,
"player_width": 529,
"player_height": 716,
"guide_id": "e89279186",
"is_direct": false }] }

我的第一种方法是使用给定的URL设置一个AVPlayer,但该URL无法播放.

My first approach was to setup an AVPlayer with one of the URLs given but it didn't play.

此外,如果您将网址复制并粘贴到chrome浏览器中,它将开始下载耗时不止的文件.

Furthermore, If you copy and paste a URL in a chrome browser it starts downloading a file that takes forever.

在检查Wireshark时,我终于发现流URL是一个不同的URL,它是在您第一次请求文件时随Header提供的.

Inspecting with wireshark, I finally found that the streaming URL is a different one and it is provided with the Header on the first time you request a file.

我知道这已经在Android上通过截取标头完成了,但是,是否有可能在iOS上做同样的事情?

I know that has been done on android by intercepting the headers but, Is is possible to do the same on iOS?

我一直在寻找一些库,并且 MobileVLCKit 似乎是一个候选人,但不确定如何冒被拒绝的风险.

I have been looking some libraries and MobileVLCKit seems a candidate but not sure how I am risking the possibility of my app getting rejected.

由于流媒体是当今非常流行的动作,我能以"Apple方式"实现吗?

Since, streaming is very popular action nowadays, Can I achieve that in an "Apple way"

谢谢

推荐答案

所有想要从TUNEIN流式传输的人

事实证明,如果您像这样设置AVPlayer:

Turns out that if you setup AVPlayer like that:

class ViewController: UIViewController, AVAssetResourceLoaderDelegate {

    let urlFile = URL(string:"http://opml.radiotime.com/Tune.ashx?id=s150147&formats=aac,mp3")!

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

    override func viewDidLoad() {
        super.viewDidLoad()

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

        let audioURLAsset = AVURLAsset(url: urlFile)
        //audioURLAsset.resourceLoader.setDelegate(self, queue:DispatchQueue.init(label: "MY QUEUE"))
        avPlayer = AVPlayer(playerItem: AVPlayerItem(asset: audioURLAsset))

        avPlayer.play()

    }

}

它将播放,但是您需要在info.plist中允许任意加载

It will play but you need to allow Arbitrary loads in the info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

而且有效!

添加

我还发现某些链接无法播放,特别是那些包含.pls文件的链接(尽管URL保持不变). iOS和Safari都尝试解决它们,但是插件不兼容存在错误.

I have also find out that some of the links are not playable, specially the ones that contains .pls files (URL remains the same though). Both iOS and safari try to resolved them but there is an error about plugin incompatibility.

我发现,如果我对URL发出URL请求,则返回的数据是由线制动器分隔的链接的列表.

I found out that if I make a URL Request to the URL, the data back is a list of links separated by line brakes.

所以: 我的连接助手类:

So: My Connection Helper class:

class URLConnectionHelper: NSObject {

    static func getResponse(fromURL url:URL?)->Data?{

        guard let url = url else{ return nil }

        var result:Data? = nil
        let semaphore = DispatchSemaphore(value: 0)

        URLSession.shared.dataTask(with: url) { (data, response, error) in

            //guard let data = data else{ return nil}
            result = data
            semaphore.signal()

            }.resume()

        semaphore.wait()

        return result

    }

}

然后从类中调用函数:

guard let dataRadioItem = URLConnectionHelper.getResponse(fromURL: urlFile) else {return}

    var urlArray = [URL]()
    if let stringData = String(data: dataRadioItem, encoding: String.Encoding.utf8){
        print("String Data: \(stringData)")

        stringData.enumerateLines { (line, _) -> () in
            if let urlLine = URL(string: line){
                print("added = \(line)")
                urlArray.append(urlLine)
            }

        }

    }

    if urlArray.count > 0{
        urlFile = urlArray.first!
    }

字符串数据的结果

String Data: http://player.absoluteradio.co.uk/tunein.php?i=a664.aac
http://player.absoluteradio.co.uk/tunein.php?i=a6.mp3
http://player.absoluteradio.co.uk/tunein.php?i=a6low.mp3
http://player.absoluteradio.co.uk/tunein.php?i=a624.aac

这篇关于播放直播的TuneIn Radio URL iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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