Thursday, 24 October 2013

Play Video from URL

In so many application we can see videos are playing from the Url,we here implement that functionality in our application.

Please first define IS_IPHONE_5 macro in .pch file so you can check it it is iphone 5 then set player size according to it.


#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
  • We have to add MediaPlayer framework and import 
    #import <MediaPlayer/MediaPlayer.h> in .h file
  • We have to define variable of activity indicator and mediaPlayer object first in .h file like
      MPMoviePlayerController *moviePlayer;
      UIActivityIndicatorView *aiv;

     first create playMovieButtonPressed to create activity indicator and movie player object and set frame according to iphone 5 and other and added as a subview of current view.we create notification which notifies us when movie player finished playing.

-(IBAction)playMovieButtonPressed:(id)sender{
    
    aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    aiv.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    aiv.center = self.view.center;
    [self.view addSubview:aiv];
    [aiv startAnimating];
    
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    NSURL *movieURL = [NSURL URLWithString:strVideo];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    [self setWantsFullScreenLayout:YES];
    [self.moviePlayer.view setFrame:self.view.bounds];
    if( IS_IPHONE_5 )
    {
        [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 568)];
    }
    else{
        [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 480)];
    }
    [self.view addSubview:self.moviePlayer.view];
    
    
    //insert the video player below the aiv
    [self.view insertSubview:self.moviePlayer.view belowSubview:aiv];
    [self.moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer];
}
#pragma Player methods
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
    {
        
    }
    NSDictionary *notiUserInfo = [notification userInfo];
    if (notiUserInfo != nil)
    {
        NSError *errorInfo = [notiUserInfo objectForKey:@"error"];
        if ([[errorInfo domain] isEqualToString:@"MediaPlayerErrorDomain"])
        {
        }
        else{
            [self dismissModalViewControllerAnimated:YES];
        }
    }
    else{
        [self dismissModalViewControllerAnimated:YES];
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
    [player.view removeFromSuperview];
    [player stop];
    player = nil;
    [aiv stopAnimating];
    [aiv removeFromSuperview];
}

here we set full screen mode of movieplayer,when done button pressed or  becuase of error movie player stops moviePlayBackDidFinish method called.

You can grab whole code here:

.h file

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface VideoPlayViewController : UIViewController
{
    MPMoviePlayerController *moviePlayer;
    UIActivityIndicatorView *aiv;
    NSString *strVideo;
}
@property (readwrite, retain) MPMoviePlayerController *moviePlayer;

-(IBAction)playMovieButtonPressed:(id)sender;
@end

.m file

#import "VideoPlayViewController.h"

@interface VideoPlayViewController ()

@end

@implementation VideoPlayViewController
@synthesize moviePlayer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieLoadStateDidChange:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:nil];
    strVideo = @"http://flierz.wwhnetwork.net/seller/upload/video/233_125_best-funny-video-ever_06112013194721_07192013183307.mp4";
    [self playMovieButtonPressed:nil];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)movieLoadStateDidChange:(id)sender{
    NSLog(@"STATE CHANGED");
    if(MPMovieLoadStatePlaythroughOK ) {
        NSLog(@"State is Playable OK");
        NSLog(@"Enough data has been buffered for playback to continue uninterrupted..");
        aiv.hidden = YES;
        [aiv stopAnimating];
    }
    
}
-(IBAction)playMovieButtonPressed:(id)sender{
    
    aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    aiv.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    aiv.center = self.view.center;
    [self.view addSubview:aiv];
    [aiv startAnimating];
    
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    NSURL *movieURL = [NSURL URLWithString:strVideo];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    [self setWantsFullScreenLayout:YES];
    //MPMovieControlStyleFullscreen;
    [self.moviePlayer.view setFrame:self.view.bounds];
    if( IS_IPHONE_5 )
    {
        [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 568)];
    }
    else{
        [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 480)];
    }
    [self.view addSubview:self.moviePlayer.view];
    
    
    //insert the video player below the aiv
    [self.view insertSubview:self.moviePlayer.view belowSubview:aiv];
    [self.moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer];
}
#pragma Player methods
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
    {
        
    }
    NSDictionary *notiUserInfo = [notification userInfo];
    if (notiUserInfo != nil)
    {
        NSError *errorInfo = [notiUserInfo objectForKey:@"error"];
        if ([[errorInfo domain] isEqualToString:@"MediaPlayerErrorDomain"])
        {
        }
        else{
            [self dismissModalViewControllerAnimated:YES];
        }
    }
    else{
        [self dismissModalViewControllerAnimated:YES];
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
    [player.view removeFromSuperview];
    [player stop];
    player = nil;
    [aiv stopAnimating];
    [aiv removeFromSuperview];
}
#pragma-mark orientation methods
- (NSUInteger) supportedInterfaceOrientations
{
//to support all orientation
    return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return TRUE;
}
@end

No comments:

Post a Comment