Ravi
Ravi

Reputation: 848

iOS- MPMoviePlayerController uable to play video from remote URL

I am trying to play video from remote URL. But it is not working. My code is :

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"start playing");

    NSURL *nsURL= [[NSURL alloc]initWithString:@"http://my_server_path:8080/content-service/v1/video/12/RGl1Nm1ib1VLMitFUmM5bEZzYVpxVGllM2RrWUw3Y09yMzdVZ0pzYlEvYjNaYUI5bEQvZERhTUhNTjBOaW5lY1hqYlZSRUM5anAvL3FsUTA2NzEwN2NMM2dnUnkxR0s4QUVyMnV2MlBLMjA9?thumb=false"];

    videoPlayer =  [[MPMoviePlayerController alloc]
                    initWithContentURL:nsURL];

    [videoPlayer.view setFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
    videoPlayer.controlStyle = MPMovieControlStyleDefault;
    videoPlayer.movieSourceType = MPMovieSourceTypeStreaming;//MPMovieSourceTypeFile/MPMovieSourceTypeUnknown ;
    [self.view addSubview:videoPlayer.view];
    [videoPlayer prepareToPlay];
    [videoPlayer play];

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

    videoPlayer.shouldAutoplay = YES;
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

    NSError *error = [[notification userInfo] objectForKey:@"error"];
    if (error) {
        NSLog(@"Did finish with error: %@", error);
    }
}

I am getting this message

Did finish with error: Error Domain=MediaPlayerErrorDomain Code=-11850 "Operation Stopped" UserInfo=0x15e77b20 {NSLocalizedDescription=Operation Stopped}

When I copy/paste my video URL in a browser it downloads successfully (even not buffers in browser) and after finished download it plays fine in laptop. I am frustrated, PLEASE HELP, any suggestion will be great, Thanks in advance

Upvotes: 4

Views: 757

Answers (3)

user3182143
user3182143

Reputation: 9609

If video is not playing on device, your HTTP server does not support byte-range requests.

HTTP servers hosting media files for iOS must support byte-range requests, which iOS uses to perform random access in media playback. (Byte-range support is also known as content-range or partial-range support.) Most, but not all, HTTP 1.1 servers already support byte-range requests.

Source From Safari Web Service Content

Source From Byte-Range Request

Solution

Upvotes: 2

JigneshP
JigneshP

Reputation: 344

Replace your url in this code with fileUrl

    MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
    mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
    [mpvc.moviePlayer setContentURL:fileURL];
    [self presentMoviePlayerViewControllerAnimated:mpvc];

Upvotes: 0

Sujit Baranwal
Sujit Baranwal

Reputation: 91

Use this code:

where fileURL is URL of video on server

    MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    if (tmpMoviePlayViewController) {
        tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:tmpMoviePlayViewController animated:YES completion:nil];
        [tmpMoviePlayViewController.moviePlayer prepareToPlay];
//        tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
        tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        [tmpMoviePlayViewController.moviePlayer play];

    }

Upvotes: 0

Related Questions