AVPlayer 流媒体工作,但它不
AVPlayer streaming works and yet it doesn't
运行 Xcode El Capitan 下的 7.1.1 10.11.2 和 IOS 9.2 应用程序
试图了解实现视频流播放所需的最少代码,并在此处制作了这个非常简单的部分……严格来说不需要观察者,但它悄悄出现了,所以我离开了它。
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 演示流,但 none 我已经尝试过其他代码。
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 真的可以玩; "fails" 我尝试的其他一切...
所以....所有这些都失败了...
//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 中需要这个密钥来播放任意流。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我知道这有点松懈,但我让 reader 做更多的研究,如果他们想让他们的应用程序更加防弹,我现在很关心 :) 这样做,使用EDITED 部分中的观察者代码,并删除主代码中的 [self.avPlayer play](第 8 行) .
运行 Xcode El Capitan 下的 7.1.1 10.11.2 和 IOS 9.2 应用程序
试图了解实现视频流播放所需的最少代码,并在此处制作了这个非常简单的部分……严格来说不需要观察者,但它悄悄出现了,所以我离开了它。
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 演示流,但 none 我已经尝试过其他代码。
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 真的可以玩; "fails" 我尝试的其他一切...
所以....所有这些都失败了...
//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 中需要这个密钥来播放任意流。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我知道这有点松懈,但我让 reader 做更多的研究,如果他们想让他们的应用程序更加防弹,我现在很关心 :) 这样做,使用EDITED 部分中的观察者代码,并删除主代码中的 [self.avPlayer play](第 8 行) .