【问题标题】:AVPlayer streaming works and yet it doesn'tAVPlayer 流媒体工作,但它没有
【发布时间】:2016-01-03 16:31:36
【问题描述】:

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

试图了解实现视频流播放所需的最少代码,并在这里制作了这个非常简单的部分......严格来说不需要观察者,但它悄悄进入,所以我离开了。

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 提供的演示流上,我没有给它播放其他任何东西...

** 尝试过 **

也尝试将此代码添加到混合中,这也适用于 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];

.......

** 更多更新 ** ...重新设计了观察者,因为我没有从中获得有用的信息,现在它告诉我苹果 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;

}

【问题讨论】:

  • 确保您提供的文件路径正确。
  • Zachary,我尝试了一些教程示例和 youtube,但没有任何效果。我正在流式传输,而不是从文件下载/播放。我有工作。
  • 也尝试通过一个avasset,它适用于Apple演示,但没有别的......
  • 更新了观察者代码;现在似乎准确地报告了,除了 Apple 演示之外,所有内容都无法流式传输。

标签: ios objective-c avplayer


【解决方案1】:

唷,回忆一下以前的事情;真的没有任何借口。设法通过寻找完全不同的地方来解决它;在 info.plist 中需要这个键来播放任意流。

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

我知道这有点松懈,但如果他们想让他们的应用程序更加防弹,我现在就让读者做更多的研究:) 这样做,使用 EDITED 部分并在您的业务中删除主代码中的 [self.avPlayer play](第 8 行)。

【讨论】:

  • 警告:自 2018 年起,如果您允许任意加载,请准备好用正当理由向 Apple 解释原因。如果理由对他们来说不够充分,那么您的应用就会被拒绝。简而言之,除非绝对必要,否则不要允许任意加载。
猜你喜欢
  • 2023-03-09
  • 2016-09-13
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2018-06-15
  • 2018-04-13
  • 2015-08-02
  • 1970-01-01
相关资源
最近更新 更多