2

I want to play a video on a same view on a button click.

NSURL *movieUrl = @"Some Url";
            CGRect frame;
            frame =CGRectMake(10,10,100,100);
            MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
            [self.view addSubview:player.view];
            player.view.frame = frame;
            [player release];

when I run this, it shows a frame with black background but does not play the video. I am not using youtube video.

Saad Ur Rehman
  • 798
  • 1
  • 10
  • 19

2 Answers2

8
NSURL* videoURL = [NSURL URLWithString:url];
MPMoviePlayerController* mPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[mPlayer prepareToPlay];
[mPlayer play];

//For viewing partially.....
[mPlayer.view setFrame:CGRectMake(50, 200, (self.view.frame.size.width)-100 , 400)];
mPlayer.view.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:mPlayer.view];  
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
Furqi
  • 2,403
  • 1
  • 26
  • 32
1

First of all, you need to call:

[player play];

Second, releasing the player will most likely stop playback immediately. You need to hang onto that player elsewhere and release it when the movie finishes playing.

hundreth
  • 841
  • 4
  • 8