Thursday, February 21, 2013

thumbnail

Video Streaming and Playing in iOS objective-C


- (void) PlayVideo:(UIButton *)sender{
   
    NSString *fileName = @"URLSOURCE";
       
    NSURL *streamURL = [NSURL URLWithString:fileName];
    
   mPlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
    
    
    [self.view addSubview:mPlayerVC.view];
    
    //play movie
    
    MPMoviePlayerController *player = [mPlayerVC moviePlayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    player.view.frame = self.view.frame;
    [player setFullscreen:YES animated:YES];
    [self.view addSubview:player.view];
    [player prepareToPlay];
    [player play];
    

}


//============Other Methods====================

- (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
    NSLog(@"enteredFullscreen");
}

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");
}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");
    [self.mPlayerVC.view removeFromSuperview];
    self.mPlayerVC = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackFinished. Reason: Playback Ended");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playbackFinished. Reason: Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            NSLog(@"exitedFullscreen");
         
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            break;
        default:
            break;
    }
    
    [self.mPlayerVC.view removeFromSuperview];
    self.mPlayerVC = nil;

}


Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

comments

About me

simple one.