AVPlayer不会在iOS 10上立即播放视频,而只播放音频

时间:2016-09-29 05:33:30

标签: ios objective-c avplayer ios10 avassetexportsession

我正在使用AVAssetExportSession创建视频并在完成后播放视频。 但是Visual Part不会立即显示但只有音频会立即播放。 视觉部分经过一段约20至30秒的延迟后出现。这是我播放视频的代码

-(void)playUrl:(NSURL *)vUrl{

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[_avPlayer currentItem]];


_avAsset=nil;
_avPlayerItem=nil;
_avPlayer =nil;
[_avPlayerLayer removeFromSuperlayer];
_avPlayerLayer=nil;

_avAsset=[AVAsset assetWithURL:vUrl];
_avPlayerItem =[[AVPlayerItem alloc]initWithAsset:_avAsset];
_avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem];
[_avPlayer replaceCurrentItemWithPlayerItem:_avPlayerItem];
_avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:_avPlayer];
[_avPlayerLayer setFrame:CGRectMake(0, 0, viewAVPlayer.frame.size.width, viewAVPlayer.frame.size.height)];
[viewAVPlayer.layer addSublayer:_avPlayerLayer];
[_avPlayer seekToTime:kCMTimeZero];

[_avPlayer play];

_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;



[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(repeatPlayer:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_avPlayer currentItem]];



}

如果有人知道答案,请告诉我。这段代码在iOS 9中完美运行,但不是iOS 10.在此先感谢。

4 个答案:

答案 0 :(得分:6)

尝试将AVPlayer的automaticWaitsToMinimizeStalling属性设置为NO,以便立即开始播放。

_avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem];
_avPlayer.automaticallyWaitsToMinimizeStalling = NO;

但如果没有足够的内容可供播放,那么播放器可能会失速。

Apple文档:https://developer.apple.com/reference/avfoundation/avplayer/1643482-automaticallywaitstominimizestal

希望这有帮助。

答案 1 :(得分:1)

我接下来会这样做:

<强>第一

我添加了观察者

- (void)attachWatcherBlock {
    [self removeWatcherBlock];
    if (self.videoPlayer) {
        __weak typeof(self) wSelf = self;
        self.timeObserver = [self.videoPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
            if (wSelf.playerBlock && wSelf.videoPlayer) {
                CGFloat playTime = CMTimeGetSeconds(wSelf.videoPlayer.currentTime);
                CGFloat duration = CMTimeGetSeconds(wSelf.videoPlayer.currentItem.duration);
                if (playTime > 0.0f) {
                    [wSelf replaceCoverToVideo];
                }
                wSelf.playerBlock(wSelf, playTime, duration);
            }
        }];
    }
 [self.videoPlayer play];
}

然后如果在block playTime中等于持续时间调用重播

- (void)replay {
    __weak typeof(self) wSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong typeof(wSelf) self = wSelf;
        if (self.videoPlayer) {
            [self.videoPlayer seekToTime:kCMTimeZero];
        }
    });
}

所有这些都在我的UIView子类中称为 NDVideoPlayerView

答案 2 :(得分:1)

我面临同样的问题,我的解决方案是将旧代码带入主线程:

        -(void)ExporterManager:(DoCoExporterManager *)manager DidSuccessComplementWithOutputUrl:(NSURL *)outputUrl{
//...
dispatch_async(dispatch_get_main_queue(), ^{
            [_playView setContentUrl:outputUrl.path];
        });
//...
    }

我使用exportAsynchronouslyWithCompletionHandler来处理我的视频。有人认为AVVideoCompositionCoreAnimationTool是问题https://forums.developer.apple.com/thread/62521的原因。 我不确定,但我确实使用它。

试试吧!

希望这有帮助!

答案 3 :(得分:1)

AVAideoCompositionCoreAnimationTool在AVAssetExportSession中使用时会干扰iOS 10.0 - 10.1.1中的AVPlayer。 iOS 10.2修复了这个错误,你的代码应该正常工作。