0

I am using AVPlayer for playing remote .mp4 files (in Objective C). The video loads from remote url, but after 1 sec of playing the player freeze. The video got stucked and not playing it.

Can any one please help me?

 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");

    }
  }
}
Rupshikha
  • 213
  • 1
  • 3
  • 16
  • 1
    You need to add more information. There is not enough information to even begin to try to find the problem. Post code, debug console result, and/or screenshots. Thank you. – lostAtSeaJoshua Dec 18 '15 at 19:22
  • ok. I am Posting my code. – Rupshikha Dec 18 '15 at 19:25
  • @lostAtSeaJoshua Hi, I have added my code. Can you please help me? – Rupshikha Dec 18 '15 at 19:27
  • In observeValueForKeyPath you are calling [self.player play] again if status is ready but make sure the player is not already playing before making this call again. You can check the property rate on the player to see if it playing. – lostAtSeaJoshua Dec 18 '15 at 19:36
  • Thank you. Can you provide me any sample code for it? – Rupshikha Dec 18 '15 at 19:38
  • observeValueForKeyPath is called for the first time, and at that time rate is 1. After that , this observer does not called again. – Rupshikha Dec 18 '15 at 19:53

0 Answers0