IOS MPMoviePlayerViewController无休止地加载

时间:2013-04-08 20:57:31

标签: ios media-player mpmovieplayercontroller video-player

我正在使用IOS应用程序(使用Storyoard)。我有一个ViewController:

//movieViewController.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface movieViewController : UIViewController {

    MPMoviePlayerViewController *moviePlayer;

}

@property (strong, nonatomic) MPMoviePlayerViewController *moviePlayer;
-(void) playMovie;

@end


//movieViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    [self playMovie];
}

-(void)playMovie
{
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"movie"
                                                      ofType:@"mov"];

    moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL
                                                     fileURLWithPath:videoPath]];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}

当我运行时,我看到我的viewController带有“正在加载......”,这需要永远。 enter image description here

2 个答案:

答案 0 :(得分:1)

您应该致电-prepareToPlay和/或-play。根据{{​​3}}的文档:

  

从iOS 5.0开始,为了便于更细粒度的播放控制,新的电影播放器​​不会自动准备播放。

答案 1 :(得分:0)

-(IBAction)startVideo:(id)sender
{
    NSURL *video = [[NSURL alloc] initWithString:@"http://www.w3schools.com/html/movie.mp4"];
    MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc]initWithContentURL:video];
    controller.view.frame = CGRectMake(20, 50, 280, 180);

    controller.moviePlayer.view.center = self.view.center;
    [self.view addSubview:controller.view];

    [controller.moviePlayer prepareToPlay];
    [controller.moviePlayer play];
}