iOS AVPlayer卡住了

时间:2015-12-18 19:20:09

标签: ios avplayer avplayerlayer

我正在使用AVPlayer播放远程.mp4文件(在Objective C中)。视频从远程网址加载,但播放播放器1秒后冻结。视频被卡住而没有播放。

任何人都可以帮助我吗?

 NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 NSURL *videoUrl = [[NSURL alloc]initWithString:encodedString];
 self.asset = [AVAsset assetWithURL:videoUrl];
 self.player = [AVPlayer playerWithURL:videoUrl];
 self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

 self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
 self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[self.player currentItem]];


self.playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.myPlayerView.layer addSublayer:self.playerLayer];


self.videoPlaybackPosition = 0;

[self.player play];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

   if (object == self.player && [keyPath isEqualToString:@"status"]) {
    if (self.player.status == AVPlayerStatusFailed) {
        NSLog(@"AVPlayer Failed --> %ld",(long)self.player.currentItem.status);

    } else if (self.player.status == AVPlayerStatusReadyToPlay) {
        NSLog(@"AVPlayerStatusReadyToPlay");

        [self.player play];


    } else if (self.player.status == AVPlayerItemStatusUnknown) {
        NSLog(@"AVPlayer Unknown");

    }
  }
}

0 个答案:

没有答案