AVPlayer流式传输有效,但它没有

时间:2016-01-03 16:31:36

标签: ios objective-c avplayer

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

试图理解我为视频流实现回放所需的最少代码,并在这里简单地制作了这个......严格来说,并不需要观察者,但它悄悄进入,所以我离开了它。 / p>

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提供的演示流上,没有别的我给它播放......

** TRIED **

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

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真正发挥作用;并且"失败"在其他一切我尝试...

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

 //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;

}

1 个答案:

答案 0 :(得分:1)

Phew,先回忆一下这件事;真的没有借口。管理通过寻找完全不同的地方来修复它;在info.plist中需要使用此键来播放任意流。

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

我知道这有点松懈,但是如果他们想让他们的应用程序更加明确我现在关心的问题,我会让读者做更多的研究:)这样做,使用已编辑部分,并在您的业务主代码中删除 [self.avPlayer play] (第8行)。