AVPlayer流可以工作,但不能 [英] AVPlayer streaming works and yet it doesn't

查看:114
本文介绍了AVPlayer流可以工作,但不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在El Capitan 10.11.2和IOS 9.2应用程序下运行Xcode 7.1.1

Running Xcode 7.1.1 under El Capitan 10.11.2 an IOS 9.2 app

试图了解实现视频流播放所需的最少代码,并在此处制作非常简单的代码...严格来说,不需要观察者,但是它悄悄潜入其中,所以我就把它留了下来.

Trying to understand the minimum code I need to implement playback for a Video stream, and crafted this very simply piece here ... don't need the Observer strictly speaking, but it crept in so I left it.

static const NSString *ItemStatusContext;
// a class static

self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
[self.avPlayer addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];

self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
avPlayerLayer.frame = CGRectMake(128, 128, 512, 386);
[self.view.layer addSublayer: avPlayerLayer];
[self.avPlayer play];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {

if (context == &ItemStatusContext ) {
    AVPlayer *thePlayer = (AVPlayer *)object;
    if ([thePlayer status] == AVPlayerStatusFailed) {
        NSError *error = [self.avPlayer error];
        // Respond to error: for example, display an alert sheet.
        NSLog(@"error %@",error);
        return;
    }
    NSLog(@"player status %ld",(long)[thePlayer status]);
    // Deal with other status change if appropriate.
}
// Deal with other change notifications if appropriate.
//[super observeValueForKeyPath:keyPath ofObject:object
 //                      change:change context:context];
return;
}

它可以工作,但是...仅在Apple提供的演示流上,我没有给它播放其他内容...

It works, but... only on the demo stream provided by Apple, nothing else I give it plays ...

**尝试**

也尝试将此代码添加到混合中,这也适用于Apple演示流,但是我没有尝试过其他代码.

Tried adding this code into the mix too, which also works with the Apple demo stream, but none of the others I have tried.

NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:url options:nil];

avPlayerItem = [[AVPlayerItem alloc] initWithAsset:avasset];
self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];

.......

** 更多更新 ** ...因为我没有从中获得有用的信息,所以重新设计了观察器,现在它告诉我Apple m3u8确实可以玩;并在我尝试的其他所有内容上均失败" ...

** MORE UPDATES ** ... reworked observer since I wasn't getting useful info from it, now it tells me the Apple m3u8 is really to play; and "fails" on everything else I try...

所以....所有这些都失败了,例如...

So .... all these fail for example ...

 //self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://content.uplynk.com/209da4fef4b442f6b8a100d71a9f6a9a.m3u8"]];
//self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://content.jwplatform.com/manifests/vM7nH0Kl.m3u8"]];
//self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://walterebert.com/playground/video/hls/sintel-trailer.m3u8"]];

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {


if (object == self.avPlayer.currentItem && [keyPath isEqualToString:@"status"]) {

    if (avPlayer.currentItem.status == AVPlayerStatusFailed) {
        NSError *error = [self.avPlayer error];
        // Respond to error: for example, display an alert sheet.
        NSLog(@"AVPlayerStatusFailed error %@",error);
        return;
    }
    if (avPlayer.currentItem.status == AVPlayerStatusUnknown) {
        NSError *error = [self.avPlayer error];
        NSLog(@"AVPlayerStatusUnknown error %@",error);
    }
    if (avPlayer.currentItem.status  == AVPlayerStatusReadyToPlay) {
        NSLog(@"AVPlayerStatusReadyToPlay");
        [self.avPlayer play];
    }
    //[super observeValueForKeyPath:keyPath ofObject:object
             //              change:change context:&ItemStatusContext];
    // Deal with other status change if appropriate.
}
// Deal with other change notifications if appropriate.
//[super observeValueForKeyPath:keyPath ofObject:object
 //                      change:change context:context];

return;

}

推荐答案

亲爱的,以前记得一些有关此的东西;真的没有任何借口.设法通过寻找完全不同的地方来修复它;在info.plist中,需要此键才能播放任意流.

Phew, recall something about this before; no excuses really. Managed to fix it by looking somewhere completely different; in info.plist which needs this key in it to play arbitrary streams.

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

我知道它有点松懈,但是如果他们想让我的应用程序更安全,我会让读者多做一些研究:)为此,请使用 EDITED 部分,并在您要从事的主要代码中切出 [self.avPlayer播放] (第8行).

I know it is bit lax, but I leave the reader to do so some more research if they want to make their app more bullet proof that I care about right now :) Do this, use the observer code in the EDITED section and cut out the [self.avPlayer play] (line 8) in the main code your be in business.

这篇关于AVPlayer流可以工作,但不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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