我必须从服务器访问视频,我希望能够实时访问它的帧,以便我可以对它们进行一些图像处理。
我知道如何使用 AVAssetReader 离线完成,但此解决方案不适用于实时处理。
这是在我的代码中,从 url 播放的样子:
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)
}
Run Code Online (Sandbox Code Playgroud)
就像上面解释的代码片段一样,在调用该.play()
函数后,我正在调用startListeningForStreamFail()
,它将当前视图控制器注册到主线程上的两种类型的通知。
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)
} …
Run Code Online (Sandbox Code Playgroud) 当我尝试在 Mac OS Catalina 上运行 PDI 时,出现此错误:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
Run Code Online (Sandbox Code Playgroud)
我已尝试更新Info.plist
并Info.plist~
按照此处的建议进行更新。我还按照此处的建议将应用程序从 mac quarentine 中移除,并允许从任何地方运行应用程序(系统偏好设置 > 安全性和隐私)
我还尝试了不同版本的 PDI(8、8.1、8.2、9),但都没有成功。
我正在尝试从 URL 获取 JSON 文件并使用 Swift 返回内容。但是,代码let httpResponse = response as! NSHTTPURLResponse
在以下代码中的行失败。我在这一行遇到异常,Xcode 进入调试模式。
class func downloadJSONFile()->AnyObject
{
let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
var json:AnyObject = ""
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do{
json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
}catch {
print("Error with Json: \(error)")
}
}
} …
Run Code Online (Sandbox Code Playgroud) 我想加载这个jpeg:https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193
但我得到了错误 The operation couldn’t be completed. (NSURLErrorDomain error -1100.)
从这篇文章中,我发现错误意味着:kCFURLErrorFileDoesNotExist
.这很奇怪,因为那里肯定有一个图像.
从这篇文章来看,问题似乎与https
协议有关,但在我实现解决方案切换到之后http
,我得到了错误:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
... The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
在进行必要的plist更改后,同样的错误会继续弹出:
如何在iOS下载此图像?
UPDATE
所以https
如果我使用这段代码,我发现下载确定:
NSData* theData = [NSData …
Run Code Online (Sandbox Code Playgroud) ios ×3
swift ×3
avfoundation ×1
avplayer ×1
avplayeritem ×1
ios9 ×1
kettle ×1
macos ×1
nsurl ×1
nsurlsession ×1
pentaho ×1
video ×1