Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone AVPlayerLayer和AVPlayer_Iphone_Objective C - Fatal编程技术网

Iphone AVPlayerLayer和AVPlayer

Iphone AVPlayerLayer和AVPlayer,iphone,objective-c,Iphone,Objective C,我使用AVPlayerLayer和AVPlayer在我的应用程序中构建视频播放器 选择新视频时,我采用以下方法: //this to remove the current video if (avPlayerLayer) { [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[audioPlay

我使用
AVPlayerLayer
AVPlayer
在我的应用程序中构建视频播放器

选择新视频时,我采用以下方法:

//this to remove the current video
    if (avPlayerLayer) {
            [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[audioPlayer currentItem]];
            [avPlayerLayer.player pause];
            [avPlayerLayer removeFromSuperlayer];
            avPlayerLayer = nil;
    }

//and this is to add a new one
    audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:fileName]];
    avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:audioPlayer] retain];
    [avPlayerLayer setFrame:self.view.bounds];

    CGRect frame = avPlayerLayer.frame;
    [avPlayerLayer setFrame:CGRectMake(frame.origin.x, frame.origin.y - 30, frame.size.width, frame.size.height)];

    [[self.view layer] addSublayer:avPlayerLayer];

    [audioPlayer play];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(finishPlayingSong)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[audioPlayer currentItem]];
    [audioPlayer release];
现在,有些时候,当我执行此方法时,设备不会开始播放视频(这是同时发生的,而不是在同一视频上)。知道为什么会这样吗?我怎么处理呢

编辑
我注意到它发生在我播放5首歌曲之后。

真的没有理由仅仅为了更改输入而创建新的AVPlayer和AVPlayerLayer实例。你真正需要的是一个新的游戏机。将其分配给当前的AVPlayer,并继续使用现有的AVPlayerLayer。

内存泄漏。每次使用
alloc:init:
创建
AVPlayer
,并将其分配给
audioPlayer
。这使其保留计数为1。然后创建一个
AVPlayerLayer
,再次增加其保留计数

稍后,您将释放
avPlayerLayer
,它会减少
audioPlayer
上的保留计数,但它永远不会回到零,因此永远不会被释放

泄漏很严重,但你也遇到了另一个问题。iOS中有一个基础设施限制。创建第五个渲染管道时,它无法获取渲染管道,因此播放失败

要解决问题,请更正内存泄漏

audioPlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:fileName]];

使用
playerWithURL:
静态工厂构造函数将隐式执行
自动释放:
,减少该对象的保留计数并避免泄漏。

一般来说,要自定义MPMovie播放器,最好使用带有AVPlayer的AVPlayerLayer


通过使用此自定义播放机,您可以根据需要自定义播放机控件(播放/暂停、后退、前进、停止和音量等)。

当avplayer可以使用代理方法时,为什么要使用通知?尝试将audioplayer.delegate=nil,同时将audioplayer设置为nil,并在初始化新播放器时,将委托设置为self.AVPlayer有委托方法吗?你确定吗?抱歉,这是我的困惑,它的AVAudioplayer具有委托方法。