Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在AVPlayer中停止视频?_Objective C_Avfoundation_Avplayer - Fatal编程技术网

Objective c 如何在AVPlayer中停止视频?

Objective c 如何在AVPlayer中停止视频?,objective-c,avfoundation,avplayer,Objective C,Avfoundation,Avplayer,我正在使用此代码使用avplayer播放视频文件如何停止它 [videoView setHidden:NO]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path2 = [document

我正在使用此代码使用avplayer播放视频文件如何停止它

 [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];
我试过了,没用

    //[self.videoPlayer release];

没有名为
stop
的方法。您可以暂停或将速率设置为0.0。

您可以暂停并将AVPlayer对象值设置为零

在您的代码中,您可以使用:

[self.videoPlayer pause];
[self.avPlayerLayer removeFromSuperlayer];
self.videoPlayer = nil;

如果不想将av播放器设置为零,更好的方法可能是:

videoPlayer.replaceCurrentItemWithPlayerItem(nil)

我通常寻找0.0,然后暂停。这对我来说非常合适。:)


我们有swift协议扩展-高兴

extension AVPlayer {
   func stop(){
    self.seek(to: CMTime.zero)
    self.pause()
   }
}

此选项与Swift不兼容。如果不执行此操作,
replaceCurrentItem..
我发现networkActivityIndicator永远不会消失,并且应用程序会消耗大量电池电量,即使播放器已暂停并设置为零。@HolaSoyEduFelizNavidad,是的。您可以执行
.replaceCurrentItem(with:nil)
extension AVPlayer {
   func stop(){
    self.seek(to: CMTime.zero)
    self.pause()
   }
}