-1

I am working on Video Player in iOS with MPMediaPlayer framework. I am retrieving the data from server end with JSON responser. My question is I want to Play Video when View was loaded and below is my Code

Please help me.

- (void)viewDidLoad  {

    videoURL=[NSURL URLWithString:@"http://GetLiveVideoUrl"];
    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:videoURL];
    [urlRequest addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];

    videoWebData=[[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
    mp = [[MPMoviePlayerController alloc]initWithContentURL:strURL];
    mp.view.frame = self.view.bounds; //Set the size
    self.view.backgroundColor=[UIColor clearColor];
    mp.controlStyle = MPMovieControlStyleNone;
    mp.view.userInteractionEnabled = YES;
    mp.movieSourceType = MPMovieSourceTypeStreaming;
    [self.view addSubview:mp.view]; //Show the view
    mp.scalingMode = MPMovieScalingModeFill;
    [mp setFullscreen:YES animated:YES];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSError *error1;
    NSString *stringResponse=[[NSString alloc]initWithData:responseDataVideo encoding:NSUTF8StringEncoding];
    NSLog(@"String Resonse is :%@",stringResponse);

    videoDict=[NSJSONSerialization JSONObjectWithData:responseDataVideo options:NSJSONReadingAllowFragments error:&error1];
    NSLog(@"Dictionary is :%@",videoDict);

    strVideoUrl=[NSString stringWithFormat:@"%@",[videoDict valueForKey:@"GetLiveVideoUrlResult"]];
    NSLog(@"String URL is :%@",strVideoUrl);

    strURL=[NSURL URLWithString:strVideoUrl];
    NSLog(@"string url at player :%@",strURL);
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Surya
  • 41
  • 8

3 Answers3

0

Write the Code viewDidAppear method

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
}
Suresh Thoutam
  • 258
  • 1
  • 7
0

Are you getting url or not, if yes then try like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    videoURL=[NSURL URLWithString:@"http://services.pawankalyan.tv/Service1.svc/GetLiveVideoUrl"];

    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:videoURL];        
    [urlRequest addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];        
    videoWebData=[[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    if (connection==videoWebData) {            
        responseDataVideo=[[NSMutableData alloc]init];            
        [responseDataVideo setLength:0];
    }
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (connection==videoWebData) {
        [responseDataVideo appendData:data];            
    }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSError *error1;
    NSString *stringResponse=[[NSString alloc]initWithData:responseDataVideo encoding:NSUTF8StringEncoding];        
    NSLog(@"String Resonse is :%@",stringResponse);

    videoDict=[NSJSONSerialization JSONObjectWithData:responseDataVideo options:
               NSJSONReadingMutableContainers error:&error1];
    NSLog(@"Dictionary is :%@",videoDict);

    strVideoUrl=[NSString stringWithFormat:@"%@",[videoDict valueForKey:@"GetLiveVideoUrlResult"]];
    NSLog(@"String URL is :%@",strVideoUrl);

    strURL=[NSURL URLWithString:strVideoUrl];
    NSLog(@"string url at player :%@",strURL);

    [self mediaplayermethod];
}

-(void)mediaplayermethod {

   // strURL make  gobal 
    mp = [[MPMoviePlayerController alloc]initWithContentURL: strURL];
    mp.view.frame = self.view.bounds; //Set the size
    self.view.backgroundColor=[UIColor clearColor];
    mp.controlStyle = MPMovieControlStyleDefault;
    [self.view addSubview:mp.view]; //Show the view
    mp.movieSourceType = MPMovieSourceTypeStreaming;
    mp.scalingMode = MPMovieScalingModeFill;
    [mp setFullscreen:YES animated:YES];
    mp.controlStyle=MPMovieControlStyleNone;
    [mp play];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Sport
  • 8,570
  • 6
  • 46
  • 65
0

@MurthyChimalakonda please follow the below code

Import Mediaplayer Framework

#import <MediaPlayer/MediaPlayer.h>

Take the button in .xib , Write the below code in .h file for Play Button Action and give the action to the button

- (IBAction)playMovie:(id)sender;

Write the below code in .m file for Play Button Action

-(IBAction)playMovie:(id)sender
{
   NSURL *url = [NSURL URLWithString:
      URL];//write your URL

    _moviePlayer =  [[MPMoviePlayerController alloc]
                 initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self
                   selector:@selector(moviePlayBackDidFinish:)
                   name:MPMoviePlayerPlaybackDidFinishNotification
                   object:_moviePlayer];

    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:YES];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
      removeObserver:self
      name:MPMoviePlayerPlaybackDidFinishNotification
      object:player];

    if ([player
        respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [player.view removeFromSuperview];
    }
}
Suresh Thoutam
  • 258
  • 1
  • 7