在iOS7上播放视频流

时间:2014-02-11 10:10:36

标签: ios objective-c mpmovieplayercontroller

我正在尝试在iOS上播放简单的直播链接。有一个视图控制器带有一个按钮,播放动作定义如下。

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

按下按钮时没有任何反应。我检查了链接,它工作正常。我哪里错了?

我找到了一个解决方案,告诉您创建一个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];

问题是如何调用此ViewCOntroller?

2 个答案:

答案 0 :(得分:1)

看看这个Link。问题不在于URL,而是与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
    }
}

答案 1 :(得分:1)

您的第一个解决方案也是正确的。通过这种方式,您可以灵活地为视频设置框架并放置在屏幕上的任何位置。 我猜您只是错过了将movieSourceType设置为MPMovieSourceTypeStreaming

- (IBAction)play:(id)sender {

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

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

此外,通过类似代码捕获MoviePlayer状态:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playbackFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:mp];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playbackChangeStateCallback:)
                                             name:MPMoviePlayerWillExitFullscreenNotification
                                           object:mp];