Search code examples
iphonempmovieplayer

how to stream a video in iphone


does initWithContentURL: in MPMoviePlayerController download the video file at first and then start playing or will it stream file like in youtube?

I want to play an video located in server through streaming in order not to waste of time.

How to perform streaming in iphone? Can anyone please suggest me if u know.

Thanks in advance.


Solution

  • NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
    }
    NSString *appFile;
    NSArray *myWords = [[NSString stringWithFormat:@"%@",[[videolistArray objectAtIndex:[number intValue]] valueForKey:@"video"]] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
    appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];
    NSFileManager *fileMgr=[NSFileManager defaultManager];
    if (![fileMgr fileExistsAtPath:appFile]) {
        NSData *imageData;
        NSURL *imageURL = [[[NSURL alloc] initWithString:[[videolistArray objectAtIndex:[number intValue]] valueForKey:@"video"] ] autorelease];
        if (imageURL) {
            imageData = [NSData dataWithContentsOfURL:imageURL];
        }
    
        [imageData writeToFile:appFile atomically:YES];
    }
    [pool release];
    
    
    if (spinner) {
        [spinner stopAnimating];
        [spinner removeFromSuperview];
        [spinner release];
        spinner = nil;
    }
    

    ---------------------------Above methode is for save video in file system of iphone
    -------------------- Below method to play that movie continuos

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if (!documentsDirectory) {
    
        NSLog(@"Documents directory not found!");
    
    }
    
    NSArray *myWords = [videoString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
    
    
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];
    
    NSLog(@"%@",[myWords lastObject]);
    
    NSURL *url = [NSURL fileURLWithPath:appFile];
    
    self.videoMPPlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
    
        self.videoMPPlayer.view.frame = CGRectMake(0, 0, 320, 480);
    
        self.videoMPPlayer.scalingMode = MPMovieScalingModeAspectFill;
    
        self.videoMPPlayer.controlStyle = MPMovieControlStyleNone;
    
    
        // Register for the playback finished notification
    
        [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(movieLoadStateChanges:) name:MPMoviePlayerLoadStateDidChangeNotification object:videoMPPlayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoMPPlayer];
    
    
    // Movie playback is asynchronous, so this method returns immediately.
    [self.videoMPPlayer play];