在从NavigationController中删除ViewController后,AVPlayer继续播放 [英] AVPlayer continues to play after ViewController is removed from NavigationController

查看:270
本文介绍了在从NavigationController中删除ViewController后,AVPlayer继续播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的项目中使用ARC,当我添加一个AVPlayerLayer它工作正常和dandy,但当我从我的UINavigationItem流行的UIViewController视频继续在后台播放。有谁知道你会如何处理这个?这似乎很容易我只是俯瞰。这里是我最初实例化的代码。

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.

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;

也许这也是完全错误的。我不确定什么时候使用(强)vs(弱)。

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.

推荐答案

如果avPlayerLayer是唯一与avPlayer交互的类, t需要维护一个引用它在你使用的类中的属性来呈现它(除非这个类在你共享的代码之外使用它)。事实上,这可能是为什么它不能按照你期望的方式工作。

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.

剪辑继续播放的原因(我认为)是因为播放器不是被解除分配。您可以使用类中的 strong 属性创建它,而不是由您传递给它的AVPlayerLayer类再次拥有它。因此,当AVPlayerLayer被释放时,AVPlayer丢失一个所有者。但它仍然有一个所有者(你的类),所以它不会释放,它继续播放。这里的解决方案是摆脱你的拥有属性一起为* avPlayer。你不需要它。创建AVPlayer并将其传递到AVPlayerLayer。

The reason the clip continues to play (I think) is because the player isn't being deallocated. You create it, own it with the strong property 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.

还有一些你可以做的可能修复行为但不是问题,请调用:

Something else you could do which might fix the behavior but not the problem, call:

[avPlayer pause]



在您的AVPlayerLayer的dealloc方法。

In your AVPlayerLayer's dealloc method.

Re:Strong vs. Weak引用:强引用意味着所有权。由于ARC正在管理内存,它将执行您以前在代码OR中将完成的所有[对象保留]和[对象释放],您将使用属性执行该操作,例如:

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:

@property (retain) NSObject *iAmRetainedWhenProperyIsAssigned;

因此,现在与ARC,我们简单的用户不使用在代码中或在定义属性时保留或释放。但我们仍然设计软件。 ARC是聪明的,但不够聪明,不足以推断我们定义的关系的架构含义。它仍然需要被告知,类拥有对属性引用的对象的引用。这减少了我们在最基本的术语:

So now with 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什么(保留)意味着属性pre-ARC(拥有/保留)

(strong) means to ARC what (retain) meant to properties pre-ARC (owned/retained)

(weak)意味着ARC什么(assign)意味着属性前ARC(不拥有/不保留)

(weak) means to ARC what (assign) meant to properites pre-ARC (not owned/not retained)

这篇关于在从NavigationController中删除ViewController后,AVPlayer继续播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆