Search code examples
iosobjective-cmpmovieplayercontroller

Playing a video stream on iOS7


I'm trying to play a simple live stream link on iOS. There's one view controller with a button with a play action defined as below.

- (IBAction)play:(id)sender {

    NSString *path = @"http://asish.flashmediacast.com:2135/live/International/playlist.m3u8";

    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
    [mp.view setFrame:[self.view bounds]];
    [mp prepareToPlay];
    [mp play];
}

Well nothing happens when the button's pressed. I've checked the link, it works fine. Where'm I going wrong?

I found a solution that tells you to create a MPMovieViewCOntroller

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

Question is how do I call this ViewCOntroller?


Solution

  • Look at this Link. The Problem is not with URL, its perfectly playing with MPMoviePlayerViewController.

    -(IBAction)btnVideoClicked:(id)sender
    {
        @try 
        {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
            GetVideos *obj_video = [arrVideos objectAtIndex:[sender tag]];
            MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:obj_video.VideoPath]];
            [moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
            [moviePlayerViewController.moviePlayer setShouldAutoplay:YES];
            [moviePlayerViewController.moviePlayer setFullscreen:NO animated:YES];
            [moviePlayerViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
            [moviePlayerViewController.moviePlayer setScalingMode:MPMovieScalingModeNone];
            [moviePlayerViewController.moviePlayer setUseApplicationAudioSession:NO];
            // Register to receive a notification when the movie has finished playing.  
            [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(moviePlaybackStateDidChange:)      name:MPMoviePlayerPlaybackStateDidChangeNotification    object:moviePlayerViewController];
            // Register to receive a notification when the movie has finished playing.  
            [[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(moviePlayBackDidFinish:)    name:MPMoviePlayerPlaybackDidFinishNotification     object:moviePlayerViewController];
            [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
            moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
            [moviePlayerViewController release];
            [pool release];
        }
        @catch (NSException *exception) {
            // throws exception
        }
    }