简体   繁体   中英

mediaplayer framework not playing video in iOS 6

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.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM