iOS-使用AVPlayer检测URL流是否正常 [英] iOS - Detecting if an URL stream is working or not, with AVPlayer

查看:195
本文介绍了iOS-使用AVPlayer检测URL流是否正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码中从url播放的样子:

This is how in my code, playing from url, looks like:

private func play() {
    let streamUrl = ...        
    let playerItem = AVPlayerItem(url: streamURL)
    radioPlayer = AVPlayer(playerItem: playerItem)
    radioPlayer.volume = 1.0
    do {
         try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
         try AVAudioSession.sharedInstance().setActive(true)
         UIApplication.shared.beginReceivingRemoteControlEvents()
     } catch {
         print("Error deactivating audio session.")
     }
     radioPlayer.play()
     startListeningForStreamFail()
     stopStartButton.setImage(#imageLiteral(resourceName: "pause_btn"), for: .normal)
}


就像上面的代码片段所述,在调用.play()函数之后,我调用的是startListeningForStreamFail(),它在主线程上将当前的viewcontroller注册为两种类型的通知.


Like the code snippet explains above, after calling the .play() function, I'm calling startListeningForStreamFail(), which registers the current viewcontroller to two types of notifications, on main thread.

private func startListeningForStreamFail() {
    DispatchQueue.main.async { [weak self] in
        NotificationCenter.default.addObserver(self as Any, selector: #selector(self?.playerItemFailedToPlay), name: NSNotification.Name.AVPlayerItemPlaybackStalled, object: self?.radioPlayer?.currentItem)
        NotificationCenter.default.addObserver(self as Any, selector: #selector(self?.playerItemFailedToPlay), name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime, object: self?.radioPlayer?.currentItem)
    }
}


选择器功能是这样的:


And the selector functions is this:

@objc private func playerItemFailedToPlay(notification: Notification) {
    print("playerItemFailedToPlay")
}


由于现在可以正常运行,我想通过在其网址中添加一些加号字符来测试失败.但是playerItemFailedToPlay()函数不会被调用,不会打印任何内容.


Because the stream right now works fine, I'm trying to test failure by addig some plus characters in it's url. But the playerItemFailedToPlay() functions does NOT get called, does NOT print anything.

即使仅更改了网址,也应该调用此选择器吗?

Should this selector getting called, even if only the url was changed?

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

推荐答案

我尝试在 Github 上构建项目a>方便检查

I tried to build a project on Github for an easy check

我遵循了以下步骤:

  1. 将NSAppTransportSecurity添加到info.plist中,如此答案以允许http

修剪提供的网址以删除所有空格

Trim the provided url to remove any spaces

let urlString = urlString.trimmingCharacters(in: .whitespacesAndNewlines)

检查字符串是否提供有效的链接

Check if the string provides a valid link

guard let url = URL(string: urlString) else { return complete(.unvalidURL) }

检查链接是否可播放

AVAsset(url: url).isPlayable

如果前面的任何步骤都不成功,则表明该网址无效

If any of the previous steps was not successful then it means the url is not valid

在启动可播放的链接后,我还为错误添加了一个观察者

I also added an observers for the errors after starting a playable link

NotificationCenter.default.addObserver(self, selector: #selector(itemFailedToPlayToEndTime(_:)), name: .AVPlayerItemFailedToPlayToEndTime, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(itemNewErrorLogEntry(_:)), name: .AVPlayerItemNewErrorLogEntry, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(itemPlaybackStalled(_:)), name: .AVPlayerItemPlaybackStalled, object: nil)

这篇关于iOS-使用AVPlayer检测URL流是否正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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