【发布时间】: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