簡體   English   中英

使用AVPlayer從互聯網流式傳輸視頻時,UI處於凍結狀態

[英]UI is Freezing, when streaming the videos from an internet using AVPlayer

我正在iOS平台中實現Media Player。 使用AVPlayer從互聯網流式傳輸視頻時,UI凍結存在問題。 注意:我沒有使用AVAudioPlayer,AVQueuePlayer。 以下是播放媒體的以下代碼:僅在啟動Streaming時發生UI凍結。

if(_player.rate!=0.0)
    {
        [_player pause];
        [ad.player.playerLayer removeFromSuperlayer];
        [_player replaceCurrentItemWithPlayerItem:[ AVPlayerItem playerItemWithURL:_tempURL]];
    }
    else
    {
        _player = [_player initWithURL:mediaURL];

   ad.player.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    ad.player.playerLayer.frame=ad.player.bounds;
    ad.player.playerLayer.frame=CGRectMake(0, 0, 320, 150);
    [ad.player.playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    ad.player.playerLayer.needsDisplayOnBoundsChange = YES;
    [ad.player.layer addSublayer:ad.player.playerLayer];

    [_player play];

}

我引用了以下鏈接: AVPlayer在緩沖音頻流開始時“凍結”了該應用程序,但是該鏈接建議用於AVQueuePlayer 但是我的要求是在AVPlayer中

當您開始播放視頻時,該視頻尚未下載任何數據,AVPlayer類具有一個名為prerollAtRate:completionHandler的方法,該方法從項目的當前播放時間開始加載數據,然后在完成加載嘗試后再調用completionHandler。

__weak typeof(self) weakSelf = self;  
[self prerollAtRate:0.0 completionHandler:^(BOOL finished) {
    NSLog(@"ready: %d", finished);

    // if ready call the play method
    if (finished) {
        dispatch_async(dispatch_get_main_queue(), ^{
           // call UI on main thread
           [weakSelf.player play]; 

        });
    }

}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM