在iOS sdk MPMoviePlayerController中播放视频

时间:2014-08-23 06:20:10

标签: ios mpmovieplayercontroller mpmovieplayer movieplayer

我正在尝试在iPhone中播放视频,以下是我使用过的代码。

它继续加载,但视频没有播放,我错过了什么。

播放器已启动,但它有声显示加载,从不播放视频。

我还导入了 MediaPlayer / MediaPlayer.h

-(IBAction)play:(id)sender
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"test_output" ofType:@"mov"];


NSURL *url = [NSURL URLWithString:
             moviePath];

_moviePlayer =  [[MPMoviePlayerController alloc]
                 initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:_moviePlayer];
[_moviePlayer prepareToPlay];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
     }


        - (void) moviePlayBackDidFinish:(NSNotification*)notification {
           MPMoviePlayerController *player = [notification object];
         [[NSNotificationCenter defaultCenter]
          removeObserver:self
          name:MPMoviePlayerPlaybackDidFinishNotification
          object:player];

if ([player
     respondsToSelector:@selector(setFullscreen:animated:)])
{
    [player.view removeFromSuperview];
}
 }

As shown in screenshot it keeps loading only

3 个答案:

答案 0 :(得分:2)

首先,检查您的电影文件是否已添加到项目中。

其次,尝试使用fileURLWithPath创建路径。替换以下代码

NSURL *url = [NSURL URLWithString:moviePath];

NSURL *url = [NSURL fileURLWithPath:moviePath];

答案 1 :(得分:0)

我看不到任何调用_moviePlayer.movi​​ePlayer播放方法的代码。

尝试在代码中添加以下语句。

[_ moviePlayer.movi​​ePlayer play];

在[self.view addSubview:_moviePlayer.view]之前添加此语句;

我希望这对你有用

答案 2 :(得分:0)

Buddy您的代码似乎是正确的。问题肯定在于URL。仔细检查网址是否正确。