4

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

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Ravi
  • 800
  • 2
  • 12
  • 28
  • Have you tried using a URL other than your local server? – App Dev Guy Feb 25 '16 at 09:10
  • "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" - this is because it's from your computer, not from a remote server. Your code appears fine. – App Dev Guy Feb 25 '16 at 09:16
  • DOES IT RUN ON SIMULATOR? – user3182143 Feb 25 '16 at 09:24
  • Let me check out, once I have done I will share results with you. – Ravi Feb 25 '16 at 09:52
  • Does video play it on your simulator? – user3182143 Feb 25 '16 at 10:05
  • no same issue, video does not play even in simulator. – Ravi Feb 25 '16 at 12:12
  • Hi! Have you eventually solved the issue? I am struggling with the very same problem. Thanks in advance! – Lunatic999 Mar 31 '16 at 18:11
  • 1
    @ Lunatic999, Yeah I found what was the problme for me : My HTTP server does not support byte-range requests. **Please must ensure your HTTP server support byter -range requests.** See this link for more information. **http://stackoverflow.com/questions/22250368/mpmovieplayerplaybackdidfinishnotification-is-called-immediately/26725402#26725402** – Ravi Apr 01 '16 at 06:09
  • That is my problem too :) my server doesn't support byte-range. Thanks! – Lunatic999 Apr 01 '16 at 07:41

3 Answers3

2

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

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • @ user3182143, how to know servers already support byte-range(content-range or partial-range ) requests OR NOT ?? – Ravi Feb 25 '16 at 17:15
0

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];

    }
Hima
  • 1,249
  • 1
  • 14
  • 18
  • Hi all, I run this command on termianl and got this response : curl -I my_server_path_/content-service/v1/video........... RESULT : **HTTP/1.1 200 OK Server: Apache-XX-- Content-Disposition: attachment; filename="12345" Content-Type: video/mp4 Transfer-Encoding: chunked Date: Thu, 25 Feb 2016 17:58:46 GMT** – Ravi Feb 25 '16 at 18:01
0

Replace your url in this code with fileUrl

    MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
    mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
    [mpvc.moviePlayer setContentURL:fileURL];
    [self presentMoviePlayerViewControllerAnimated:mpvc];
JigneshP
  • 344
  • 2
  • 7