MPMoviePlayerController立即结束

时间:2012-09-11 13:07:54

标签: iphone ios media-player mpmovieplayercontroller

我正在尝试从我的资源文件加载一个25秒的mp4电影,但是当我播放它时,我的MPMoviePlayerPlaybackDidFinishNotification选择器会立即被MPMovieFinishReasonPlaybackEnded调用。当我记录我的播放状态时,它会显示:

MPMoviePlaybackStatePlaying
MPMoviePlaybackStatePaused
MPMoviePlaybackStateStopped
MovieFinishReasonPlaybackEnded
MPMoviePlaybackStatePlaying
MPMoviePlaybackStatePaused

即使我只调用一次播放方法。我希望有人可以帮助我。

- 编辑以显示我的代码:

MPMoviePlayerController* player = [[[MPMoviePlayerController alloc] initWithContentURL:movieURL] autorelease];

if (player)
{

    self.moviePlayerController = player;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:player];

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerPlaybackStateDidChange:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:player];


    player.contentURL = movieURL;

    player.movieSourceType = MPMovieSourceTypeFile;

    player.controlStyle = MPMovieControlStyleNone;

    player.fullscreen = YES;

    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            player.view.transform = CGAffineTransformMakeRotation(90.0f * (M_PI / 180.0f));
            break;
        case UIInterfaceOrientationLandscapeRight:
            player.view.transform = CGAffineTransformMakeRotation(-90.0f * (M_PI / 180.0f));
            break;
        default:
            break;
    }

    player.view.frame = self.view.bounds;

    [self.view addSubview:player.view];
}

[self.moviePlayerController play]

2 个答案:

答案 0 :(得分:1)

self.moviePlayerController是保留财产吗?如果没有,MPMoviePlayerController实例将很快(autorelease)释放,您可能会遇到类似的行为。

答案 1 :(得分:0)

如果不再查看您的代码,我建议您尝试播放另一个您知道可以播放的文件。例如,从此示例项目中抓取电影:http://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html并查看它是否播放。

当我尝试播放格式不正确的文件时,我遇到了类似的事情。


嗯......我没有看到什么是错的。你确定movieURL是正确的吗?你是怎么得到的?

为了记录,这里是我如何呈现电影,虽然它与你正在做的事情没有完全相同的效果。

NSString *path = [[NSBundle mainBundle] pathForResource:movieFileName ofType:@"m4v"];

// If path is NULL (the resource does not exist) return to avoid crash
if (path == NULL)
    return;

NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerViewController *mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
mpViewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
mpViewController.moviePlayer.shouldAutoplay = YES;

// NOTE: This can sometimes crash the app in the Simulator. This is a known bug
// in xcode: http://stackoverflow.com/a/8317546/472344
[self presentMoviePlayerViewControllerAnimated:mpViewController];
[mpViewController release];