ios 从 NavigationController 中移除 ViewController 后,AVPlayer 继续播放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8073098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVPlayer continues to play after ViewController is removed from NavigationController
提问by James Parker
So I'm using ARC in my project and when I add an AVPlayerLayer it works just fine and dandy, but when I pop the UIViewController from my UINavigationItem the video continues to play in the background. Does anyone know how you would handle this? It seems like something easy I'm just overlooking. Here's the code I have for the initially instantiations.
所以我在我的项目中使用了 ARC,当我添加一个 AVPlayerLayer 时,它工作得很好,很花哨,但是当我从 UINavigationItem 弹出 UIViewController 时,视频继续在后台播放。有谁知道你会如何处理这个?这似乎很简单,我只是忽略了。这是我用于初始实例化的代码。
self.currentItem = [[AVPlayerItem alloc] initWithURL:url];
self.player = [[AVPlayer alloc]initWithPlayerItem:self.currentItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
self.avPlayerLayer.bounds = self.view.bounds;
self.avPlayerLayer.frame = CGRectMake(0,55, 1024, 670);
self.view.backgroundColor = [UIColor clearColor];
[self.view.layer addSublayer:avPlayerLayer];
Also this is how I have the properties definied.
这也是我定义属性的方式。
@property (strong) AVPlayer *player;
@property (strong) AVPlayerLayer *avPlayerLayer;
@property (strong) AVPlayerItem *currentItem;
Maybe that's entirely wrong as well. I'm not exactly sure when to use (strong) vs (weak). Any case thank you ahead of time for any help.
也许这也是完全错误的。我不确定何时使用(强)与(弱)。无论如何,请提前感谢您的帮助。
采纳答案by isaac
If avPlayerLayer is the only class interacting with the avPlayer, you don't need to maintain a reference to it with a property in the class you're using to present it (unless this class uses it outside the code you've shared). In fact, this is probably why it isn't working the way you expect.
如果 avPlayerLayer 是唯一与 avPlayer 交互的类,则您不需要使用用于呈现它的类中的属性维护对它的引用(除非该类在您共享的代码之外使用它)。事实上,这可能就是它没有按您期望的方式工作的原因。
The reason the clip continues to play (I think) is because the player isn't being deallocated. You create it, own it with the strongproperty in your class, than it's owned again by the AVPlayerLayer class you hand it to. So when the AVPlayerLayer is deallocated, the AVPlayer looses one owner. But it still has an owner (your class), so it isn't deallocated, and it keeps playing. The solution here is to get rid of your owning property altogether for *avPlayer. You don't need it. Create the AVPlayer and pass it to AVPlayerLayer. That should be all that's needed.
剪辑继续播放的原因(我认为)是因为播放器没有被释放。您创建它,使用您班级中的强属性拥有它,而不是由您将其交给的 AVPlayerLayer 类再次拥有。所以当 AVPlayerLayer 被释放时,AVPlayer 失去了一个所有者。但是它仍然有一个所有者(您的班级),因此它不会被释放,并且会继续播放。这里的解决方案是完全摆脱*avPlayer 拥有的财产。你不需要它。创建 AVPlayer 并将其传递给 AVPlayerLayer。这应该是所有需要的。
Something else you could do which might fix the behavior but not the problem, call:
您可以做的其他事情可能会解决行为但不能解决问题,请致电:
[avPlayer pause]
In your AVPlayerLayer's dealloc method.
在您的 AVPlayerLayer 的 dealloc 方法中。
Re: Strong vs. Weak references: A strong reference implies ownership. As ARC is managing memory, it'll be doing all the [object retain]ing and [object release]ing that you previously would have done in code OR that you would have done with properties, ie:
回复:强引用与弱引用:强引用意味着所有权。由于 ARC 正在管理内存,因此它将执行您以前在代码中完成的所有 [对象保留] 和 [对象释放] 处理,或者您将使用属性完成的所有 [对象保留] 和 [对象释放],即:
@property (retain) NSObject *iAmRetainedWhenProperyIsAssigned;
So nowwith ARC, we simple users don't use words like retain or release in code or when defining properties. But we still design software. ARC is smart, but not smart enough to infer the architectural implications of relationships that we're defining. It still needs to be told that a class "owns" a reference to the object that the property refers to. Which reduces us, in the most basic terms:
所以现在有了 ARC,我们简单的用户不会在代码中或在定义属性时使用诸如保留或释放之类的词。但我们仍然设计软件。ARC 很聪明,但还不够聪明,无法推断出我们正在定义的关系的架构含义。它仍然需要被告知一个类“拥有”对该属性所引用的对象的引用。用最基本的术语来说,这减少了我们:
(strong) means to ARC what (retain) meant to properties pre-ARC (owned/retained)
(强)对 ARC 意味着什么(保留)对 ARC 之前的财产(拥有/保留)意味着什么
(weak) means to ARC what (assign) meant to properites pre-ARC (not owned/not retained)
(弱)对 ARC 意味着什么(分配)对 ARC 之前的属性意味着什么(不拥有/不保留)
回答by Vijyendra Sisodia
Maybe you did not pause. Do it and then remove layer and nullify player
.
也许你没有停下来。这样做然后删除图层并取消player
。
[player Pause];
[player removefromsuperlayer];
player = nil;
回答by BigCheesy
When you want to stop an AVPlayerItem from loading, use AVQueuePlayer's removeAllItems and then re-initialize it.
当您想停止加载 AVPlayerItem 时,请使用 AVQueuePlayer 的 removeAllItems 然后重新初始化它。
[self.avPlayer removeAllItems];
self.avPlayer = [AVQueuePlayer playerWithURL:[NSURL URLWithString:@""]];
self.avPlayer = nil;
This will stop the current item from loading -- it is the only way I found to accomplish this.
这将停止加载当前项目——这是我发现的唯一方法。