How to play video stream with MPMoviePlayerController in iOS

25,914

Solution 1

Instead of creating a MPMoviePlayerController and adding that to your view, it is probably simpler to create a MPMoviePlayerViewController and present that view controller modally (since you are trying to show your video full screen anyway). Then the MPMoviePlayerViewController can manage the presentation of your video for you.

MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(moviePlaybackDidFinish:)
                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                       object:nil];    

mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

[self presentMoviePlayerViewControllerAnimated:mpvc];
[mpvc release];

In your moviePlayBackDidFinish delegate method you can then dismiss the model view controller.

Solution 2

Need to mention movie source type as streaming

moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

Solution 3

Add AVFoundation frame work in Link Libraries section

In your .h file add

#import <MediaPlayer/MediaPlayer.h>
@interface video_liveViewController : UIViewController<MPMediaPickerControllerDelegate,MPMediaPlayback>

In your .m file

NSURL *movieURL = [NSURL URLWithString:@"http://172.31.17.252:1935/live/myStream/playlist.m3u8"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];
Share:
25,914
derFalke
Author by

derFalke

Updated on February 21, 2020

Comments

  • derFalke
    derFalke over 4 years

    I am trying to play a video stream from the internet on the iPhone by pressing a button. I used many code samples but nothing worked. With this code it opens a black view without any video stream or controls in it. (The stream itself works.)

    NSURL *url = [NSURL URLWithString:@"http://MyStreamURL.com"];
    MPMoviePlayerController *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];
    
  • mostafa tourad
    mostafa tourad over 12 years
    Small correction though; you are supposed to use presentMoviePlayerViewControllerAnimated instead of presentModalViewController. This will result into the proper animation effect users are expecting from a movie player.
  • jonkroll
    jonkroll over 12 years
    Thanks @Till, I did not know that. I have edited my code sample to reflect this change.
  • Alper
    Alper almost 12 years
    But why exactly doesn't the approach above with MPMoviePlayerController work?
  • Yingpei Zeng
    Yingpei Zeng over 10 years
    @alper, it is because moviePlayer is not retained. At least it is true in my project.