【问题标题】:AVPlayer does not play video Instantly on iOS 10, while audio playing onlyAVPlayer 不会在 iOS 10 上立即播放视频,而仅播放音频
【发布时间】:2016-09-29 05:33:30
【问题描述】:

我正在使用 AVAssetExportSession 创建视频并在完成后播放视频。 但是视觉部分没有立即显示,而只有音频正在立即播放。 视觉部分在大约 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 中无法正常运行。提前致谢。

【问题讨论】:

  • 此问题的任何新答案,谢谢

标签: ios objective-c avplayer ios10 avassetexportsession


【解决方案1】:

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

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

但如果没有足够的内容可供播放,则播放器可能会停止。

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

希望这会有所帮助。

【讨论】:

【解决方案2】:

接下来我做:

第一

我添加了观察者

- (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等于duration call replay

- (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

【讨论】:

  • 我想立即播放 AVPlayer,此代码是用于此目的还是仅用于重播先生。
【解决方案3】:

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

        -(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 的原因。 我不确定,但我确实使用它。

试试吧!

希望这会有所帮助!

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2017-02-23
      • 2020-06-27
      相关资源
      最近更新 更多