找到你要的答案

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

Q:iOS:如何在您的目录中保存的视频和玩了?

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. I know how can I play a local video from the Resources, but my question is how could I download it , and the find it to be played. I am using MPMoviePlayerController to run the video.

Thanking in advance

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. I know how can I play a local video from the Resources, but my question is how could I download it , and the find it to be played. I am using MPMoviePlayerController to run the video.

感谢提前

answer1: 回答1:

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

如果你有一个有效的视频网址,苹果提供了一个API直接缓冲视频NSURL。

  1. 你应该有一个参考的mpmovieplayercontroller对象从控制器使电弧不释放对象。

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

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

    MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] initWithContentURL:url];
    
  4. 调整控制器大小,将其添加到您的视图,播放它,享受视频。

    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 
    

更多的细节可以在iOS7的URL访问这个视频播放

answer2: 回答2:

I found the answer

here I saved the video

    NSString *stringURL = @"http://videoURL";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString  *documentsDirectory ;
if ( urlData )
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   documentsDirectory = [paths objectAtIndex:0];

    NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];
    [urlData writeToFile:filePath atomically:YES];

}

and this code is for playing the video form its directory

    NSString  *filepath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];

//video URL
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];

我找到了答案

在这里我保存了视频

    NSString *stringURL = @"http://videoURL";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString  *documentsDirectory ;
if ( urlData )
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   documentsDirectory = [paths objectAtIndex:0];

    NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];
    [urlData writeToFile:filePath atomically:YES];

}

这个代码是用于播放视频的目录

    NSString  *filepath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];

//video URL
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];
ios  objective-c  media-player  mpmovieplayer