How to stop a video in AVPlayer?

前端 未结 5 583
我在风中等你
我在风中等你 2020-12-15 03:58

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

 [videoView setHidden:NO];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(N         


        
相关标签:
5条回答
  • 2020-12-15 04:31

    We have swift protocol extensions - rejoice.

    extension AVPlayer {
       func stop(){
        self.seek(to: CMTime.zero)
        self.pause()
       }
    }
    
    0 讨论(0)
  • AVPlayer does not have a method named stop. You can pause or set rate to 0.0.

    0 讨论(0)
  • 2020-12-15 04:35

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

    [self.videoPlayer seekToTime:CMTimeMake(0, 1)];
    [self.videoPlayer pause];
    
    0 讨论(0)
  • 2020-12-15 04:36

    You can pause and set AVPlayer object value to nil .

    And in your code you can use this :

    [self.videoPlayer pause];
    [self.avPlayerLayer removeFromSuperlayer];
    self.videoPlayer = nil;
    
    0 讨论(0)
  • 2020-12-15 04:39

    If you dont want to set the av player to nil, a better approach might be :

    videoPlayer.replaceCurrentItemWithPlayerItem(nil)
    
    0 讨论(0)
提交回复
热议问题