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];
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.
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.
An object that presents the visual contents of a player object.
AVPlayer does not have a method named stop
. You can pause
or set rate
to 0.0.
I usually seekToTime 0.0, then pause. It work perfectly with me. :)
[self.videoPlayer seekToTime:CMTimeMake(0, 1)]; [self.videoPlayer pause];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With