mediaplayer framework not playing video in iOS 6

2019-05-20 05:12发布

I am using the mediaplayer framework included in iOS 6 to try and play a movie from within an app. I import and then:

-(IBAction)playMovie:(id)sender
{
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];
}

The view goes to blank screen and infinite loading when this function is called. I have tried many other versions of this implementation and the results vary and all fail. The log in the is particular case is:

2012-11-05 21:19:27.900 [MPAVController] Autoplay: Disabling autoplay for pause
2012-11-05 21:19:27.902 [MPAVController] Autoplay: Disabling autoplay
2012-11-05 21:19:27.977 [MPAVController] Autoplay: Disabling autoplay for pause
2012-11-05 21:19:27.978 [MPAVController] Autoplay: Disabling autoplay
2012-11-05 21:19:27.984 [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2012-11-05 21:19:28.156 [MPAVController] Autoplay: Enabling autoplay

Got ideas on the cause? This is my first venture into playing video and it has turned out to be a nightmare at this point.

3条回答
三岁会撩人
2楼-- · 2019-05-20 05:20
Am facing the same issue, you can else try playing it on the web view. 

   NSURL *url = [NSURL fileURLWithPath:self.filePath];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.docWebView loadRequest:request];
查看更多
Emotional °昔
3楼-- · 2019-05-20 05:24

In .h file add the following

@property (nonatomic, strong) MPMoviePlayerController *controller;

try this

 -(IBAction)playMovie:(id)sender
    {
        NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"];
        NSURL *fileURL = [NSURL fileURLWithPath:filepath];
        MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
        [self.view addSubview:moviePlayerController.view];
        moviePlayerController.fullscreen = YES;
    [moviePlayerController prepareToPlay];
        [moviePlayerController play];
[self setController:moviePlayerController];
    }
查看更多
Root(大扎)
4楼-- · 2019-05-20 05:33

The problem of your code is that the variable "moviePlayerController" is just local scope, so right after you invoke [moviePlayerController play]; and exit the function the local variable was release (because there is an asyn operation here with play method to play from queue). It doesn't keep content refer to URL anymore. So you see a black screen and an infinitive "Loading..."

You need to declare an instance variable of class and copy the content from local variable to class's property, like the example code from @iAppDeveloper above. It should work!

查看更多
登录 后发表回答