AVPlayer.play may not be drawn

Asked 2 years ago, Updated 2 years ago, 35 views

What I want to do is


on iOS Play videos in AVPlayer
When played to the end
Look at the repeat flag variable
Play it over your head if necessary
That's all.

However, there is a fairly rare probability that it will occur once every few hundred times. On the screen, it stops playing until the end
However, after a few seconds of the video measurement, it starts to move again
It happens that
That's exactly the number of seconds of the video.

Why did you decide the number of seconds of the video is correct?
It runs the same code that plays the same video with multiple ipads next to it.
Start playing exactly at the same time
For example, the iPad 1 repeatedly plays a 3-second video. The iPad 2 works in the same way, but when the problem occurs,
Only iPad2 stopped at the last frame of the video for 3 seconds, and at the next loop, it started playing again from the head at the same time as iPad1.
It can be judged that the amount of time that was stopped is the measure of the video.

The video file is not online, it is in the camera roll.

Although the probability is low, I have seen the phenomenon several times and have reproduced it.

Some of the codes are as follows:I didn't do anything special...

// When playing, ViewController passes the video URL and calls it.
- (void) setPlayerWithURL: (NSURL*) url
{
    self.player=[AVPlayer alloc] initWithURL:url];
    [(AVPlayerLayer*) self.playerView.layer setPlayer:self.player];

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

// upon completion of playback
- (void)playerItemDidReachEnd: (NSNotification*)notification
{
    dispatch_async(dispatch_get_main_queue(),^{
        NSLog (@"notification retrieve");
        if(self.isRepeat)
        {
            NSLog (@"notification replay");
            self.player seekToTime:kCMTimeZero;
            [self.player play]
        }
    });
}

From the perspective of it, the playback has stopped once, but it starts to move in the next loop, so I think it's moving internally just because the drawing has stopped.

I looked into various things, but I couldn't find such a phenomenon or countermeasure.

The environment is
iPad Air 2 Wi-Fi Model 16GB MGLW2J/A
OS 8.4
That's it.

However, this is not the only symptom that occurred in the past when the OS version was older.
And the device is not limited to iPad, but also iPhone.

If you know any countermeasures, please let me know.

ios

2022-09-30 17:09

2 Answers

The possible cause is that seekToTime and play processing are performed at the same time.

How about using the following functions?

 - (void) seekToTime: (CMTime) time
 completionHandler: (void(^)(BOOL finished)) completionHandler

The procedure to explicitly play after the seek is complete.
Below is the proposed change.

// When playback is complete
- (void)playerItemDidReachEnd: (NSNotification*)notification
{
    dispatch_async(dispatch_get_main_queue(),^{
        NSLog (@"notification retrieve");
        if(self.isRepeat)
        {
            NSLog(@"notification seekToHead";
            [self.player seekToTime:kCMTimeZero]
                  completionHandler:^(BOOL finished) {
                       if(finished){
                           NSLog (@"notification replay");
                           [self.player play]
                       }
                  }];

        }
    });
}


2022-09-30 17:09

Are you playing the m3u8 video that I got from http?
The m3u8 consists of multiple files, so if you cannot retrieve any of them due to communication conditions, etc.,
AVPlayer should wait for a while to resume.

I understand that the playback stops, but is the rate of the player zero?
If it is not 0, I think it can be played internally.
Also, what is the status?
I think you should use KVO to monitor the rate and status and handle it properly.

Also, are there any errors?

NSError* error=self.player.currentItem.error;

 if(error==nil){
   AVPlayerItemErrorLog*log = [session.player.currentItemErrorLog];
   AVPlayerItemErrorLogEvent* event = [log events] [0];
   NSLog(@"Error%@",log);
   NSLog(@"Error URI%@", [event URI]);
   NSLog(@"errorStatusCode%ld", (long) [eventerrorStatusCode]);
   NSLog(@"errorDomain%@", [eventerrorDomain]);
   NSLog(@"ErrorComment%@", [event errorComment]);          
 } else {            
   NSLog(@"Error%@", error);
 }


2022-09-30 17:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.