Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a video in AVPlayer?

I am using this code to play video file using avplayer how do I stop it

 [videoView setHidden:NO];     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentsDirectory = [paths objectAtIndex:0];     NSString *path2 = [documentsDirectory stringByAppendingPathComponent:saveFileName];     NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path2];     videoPlayer = [AVPlayer playerWithURL:url1] ;     self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];      //[self willAnimateRotationToInterfaceOrientation];     avPlayerLayer.frame = videoView.bounds;     [self.videoView.layer addSublayer: avPlayerLayer];      [self.videoPlayer play]; 

I tried this it doesn't work

    //[self.videoPlayer release]; 
like image 860
iTag Avatar asked Jul 24 '13 10:07

iTag


People also ask

How to detect when AVPlayer video ends playing?

If you want to check the status of a played video - one of the best solutions is to add an observer to the AVPlayer item . The AVPlayerViewController doesn't notify about the ending of a video. This is why you need to check it by yourself. You should add the NotificationCenter observer to your playVideo() method.

How do I know if AVPlayer is playing?

And to track playing you can: "track changes in the position of the playhead in an AVPlayer object" by using addPeriodicTimeObserverForInterval:queue:usingBlock: or addBoundaryTimeObserverForTimes:queue:usingBlock:. You could throw in a Boolean flag to check the play status.

What is Avplayerlayer?

An object that presents the visual contents of a player object.


2 Answers

AVPlayer does not have a method named stop. You can pause or set rate to 0.0.

like image 121
Samet DEDE Avatar answered Oct 14 '22 19:10

Samet DEDE


I usually seekToTime 0.0, then pause. It work perfectly with me. :)

[self.videoPlayer seekToTime:CMTimeMake(0, 1)]; [self.videoPlayer pause]; 
like image 44
Huynh Inc Avatar answered Oct 14 '22 20:10

Huynh Inc