iOS: How to save a video in your directory and play it afterwards?

前端 未结 2 954
小蘑菇
小蘑菇 2021-01-06 06:25

I am working in an iOS project. I want may application to download a video from the internet programmatically. Then I want to play it.

2条回答
  •  误落风尘
    2021-01-06 07:03

    If you have a valid url of the video, Apple provides an API to directly buffer videos with NSURL.

    1. You should hold a reference to the MPMoviePlayerController object from the controller so that ARC doesn't release the object.

      @property (nonatomic,strong) MPMoviePlayerController* mc; 
      
    2. Make the URL

      NSURL *url = [NSURL URLWithString:@"http://www.example.com/video.mp4"];
      
    3. Init MPMoviePlayerController with that URL

      MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] initWithContentURL:url];
      
    4. Resize the controller, add it to your view, play it and enjoy the video.

      self.mc = controller; // so that ARC doesn't release the controller
      controller.view.frame = self.view.bounds;
      [self.view addSubview:controller.view];
      [controller play]; //Start playing 
      

    For more detail you can visit this playing video from a url in ios7

提交回复
热议问题