iOS:AVPlayerViewController 视图未删除
Posted
技术标签:
【中文标题】iOS:AVPlayerViewController 视图未删除【英文标题】:iOS: AVPlayerViewController view not removing 【发布时间】:2016-01-07 08:59:27 【问题描述】:我使用AVPlayerViewController
创建了一个示例视频应用。它工作正常。但问题是在完成视频后,AVPlayerViewController
视图没有被删除。其他问题是为AVPlayerViewController
在横向模式全屏下设置纵向默认尺寸的约束。我是新来的约束。请下载项目并运行项目记录器显示约束问题。
源码项目:http://www.filedropper.com/avplayerdemos
【问题讨论】:
【参考方案1】:我发现您的代码有两个问题:
asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^
dispatch_async(
dispatch_get_main_queue(), ^
if (!asset.playable)
return;
else
[self prepareToPlayAsset: asset withRequestedKeys: requestedKeys];
if (videoPlayerItem)
[videoPlayerItem removeObserver:self forKeyPath:kkStatusKey];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object: videoPlayerItem];
);
];
上面的这段代码你添加了观察者并在之后删除。所以应该改变两个:
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^
dispatch_async(
dispatch_get_main_queue(), ^
if (videoPlayerItem)
[videoPlayerItem removeObserver:self forKeyPath:kkStatusKey];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object: videoPlayerItem];
if (!asset.playable)
return;
else
[self prepareToPlayAsset: asset withRequestedKeys: requestedKeys];
);
];
问题 2:
- (void)prepareToPlayAsset: (AVURLAsset *)asset withRequestedKeys: (NSArray *)requestedKeys
for (NSString *thisKey in requestedKeys)
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
switch (keyStatus)
case AVKeyValueStatusUnknown:
NSLog(@"%@ AVKeyValueStatusUnknown", thisKey);
break;
case AVKeyValueStatusFailed:
NSLog(@"Error! PlayAsset failed.\nAVKey : %@.\nError: %@", thisKey, error);
return;
break;
case AVKeyValueStatusLoading:
NSLog(@"%@ AVKeyValueStatusLoading", thisKey);
break;
case AVKeyValueStatusCancelled:
NSLog(@"%@ AVKeyValueStatusCancelled", thisKey);
break;
case AVKeyValueStatusLoaded:
videoPlayerItem = [AVPlayerItem playerItemWithAsset: asset];
[videoPlayerItem addObserver:self forKeyPath: kkStatusKey options:0 context:nil];
videoPlayer = [AVPlayer playerWithPlayerItem: videoPlayerItem];
/**
* Creating the videoAdplayer through passing the avplayer object
*/
[self createVideoPlayer: videoPlayer];
if ([thisKey isEqualToString: @"duration"])
else if ([thisKey isEqualToString: @"tracks"])
NSLog(@"\n\n asset.tracks : %@ \n\n", asset.tracks);
else if ([thisKey isEqualToString: @"metadata"])
NSLog(@"\n\n assetMetadata : %@ \n\n", asset.metadata);
break;
default:
break;
if (!asset.playable)
return;
在这个循环中,只需检查一切负载,如果失败则返回。在这种情况下,有 2 个键,您这样的代码将添加两个 childviewcontroller
,它将播放两个项目的播放器。所以把代码改成它:
- (void)prepareToPlayAsset: (AVURLAsset *)asset withRequestedKeys: (NSArray *)requestedKeys
for (NSString *thisKey in requestedKeys)
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
switch (keyStatus)
case AVKeyValueStatusUnknown:
NSLog(@"%@ AVKeyValueStatusUnknown", thisKey);
break;
case AVKeyValueStatusFailed:
NSLog(@"Error! PlayAsset failed.\nAVKey : %@.\nError: %@", thisKey, error);
return;
break;
case AVKeyValueStatusLoading:
NSLog(@"%@ AVKeyValueStatusLoading", thisKey);
break;
case AVKeyValueStatusCancelled:
NSLog(@"%@ AVKeyValueStatusCancelled", thisKey);
break;
case AVKeyValueStatusLoaded:
break;
default:
break;
videoPlayerItem = [AVPlayerItem playerItemWithAsset: asset];
[videoPlayerItem addObserver:self forKeyPath: kkStatusKey options:0 context:nil];
videoPlayer = [AVPlayer playerWithPlayerItem: videoPlayerItem];
/**
* Creating the videoAdplayer through passing the avplayer object
*/
[self createVideoPlayer: videoPlayer];
if (!asset.playable)
return;
我很确定你的演示。这样改,就可以了。
【讨论】:
谢谢哥们。它正在工作。一个疑问,如果我在 AVKeyValueStatusLoaded 中编写代码有什么问题?以及你能帮助解决约束吗? 你可以检查代码添加在 AVKeyValuStatusLoaded 你会看到问题。关于约束你可以检查这个问题:***.com/questions/17772922/… 你好兄弟@vienvu,你能帮我解决我的问题***.com/questions/43132122/… 吗?以上是关于iOS:AVPlayerViewController 视图未删除的主要内容,如果未能解决你的问题,请参考以下文章
{python之IO多路复用} IO模型介绍 阻塞IO(blocking IO) 非阻塞IO(non-blocking IO) 多路复用IO(IO multiplexing) 异步IO