Search code examples
iphoneios4mpmovieplayercontrollermpmovieplayer

How to play video in iphone sdk 4.0?


I played video in iphone by using this code

-(IBAction)btnNew_clicked:(id)sender {  

NSURL *url = [NSURL URLWithString:@"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:mp];}

-(void)videoPlayBackDidFinish:(NSNotification*)notification  {       
[self dismissMoviePlayerViewControllerAnimated];  }

by using this code movie player opens for 1 second and then close automatically, without playing the file.

I checked the video file it is correct but it is not playing in iphone sdk 4.0.

Thanks in Advance.


Solution

  • please see this question-

    Playing a video file from server in an Iphone app

    trry out following code and don't forget to add MediaPlayer.framework and must import <MediaPlayer/MediaPlayer.h>

    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSURL *url = [NSURL URLWithString:@"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4"];
        MPMoviePlayerController *player =[[MPMoviePlayerController alloc] initWithContentURL: url];
        [[player view] setFrame: [self.view bounds]];  // frame must match parent view
        [self.view addSubview: [player view]];
        [player play];
    
    }