AVPlayer stops playing and doesn't resume again

前端 未结 9 1544
离开以前
离开以前 2020-12-02 06:03

In my application I have to play audio files stored on a web server. I\'m using AVPlayer for it. I have all the play/pause controls and all delegates and observ

9条回答
  •  死守一世寂寞
    2020-12-02 06:49

    In my case ,

    • I was trying to record video with imagePickerController and playback recorded video with AVPlayerController. But it starts playing video and gets stops after 1 second.Somehow it gets time to save video and if you playback it immediately, it wont play.
      So solution is ,
    • call play video after 0.5 seconds (delay). like below

         -(void)imagePickerController:(UIImagePickerController *)picker 
                  didFinishPickingMediaWithInfo:(NSDictionary *)info {
                 [self performSelector:@selector(playVideo) withObject:self 
                 afterDelay:0.5];
           }
      

      -(void) playVideo {

        self.avPlayerViewController = [[AVPlayerViewController alloc] init];
           if(self.avPlayerViewController != nil)
         {
          AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:Vpath];
          AVPlayer* player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
          self.avPlayerViewController.player = player;
          self.avPlayerViewController.showsPlaybackControls = NO;
          [self.avPlayerViewController setVideoGravity:AVLayerVideoGravityResizeAspectFill];
          [self.avPlayerViewController.view setFrame:[[UIScreen mainScreen] bounds]];
          self.avPlayerViewController.view.clipsToBounds = YES;
          self.avPlayerViewController.delegate = self;
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
          [self.viewVideoHolder addSubview:self.avPlayerViewController.view];
          [self.avPlayerViewController.player play];
      
      }
      

      }

       -(void) playerDidFinishPlaying
         {
          [avPlayer pause];
          }
      

提交回复
热议问题