0

视频文件是http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4 将其复制到应用程序包中后无法播放,然后运行:怎么了?

-(void)showvideo {
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

    [center addObserver:self selector:@selector(mPMoviePlayerLoadStateDidChangeNotification) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    [center addObserver:self selector:@selector(readytoplay) name:MPMoviePlayerReadyForDisplayDidChangeNotification object:nil];

    NSBundle* b = NSBundle.mainBundle;
    NSString* res = [b resourcePath];
    NSString* file;
    //  file = @"sr.m4v";
//  file = @"sample_iPod.m4v";
    file = @"big_buck_bunny.mp4";
//  file = @"xxx.mp4";
    NSString* path = [res stringByAppendingPathComponent:file];
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSLog(@"file exists!");
    }
    else {
        NSLog(@"file does not exist!");
    }

    NSURL* url = [NSURL URLWithString:path];
    NSLog(@"url: %@", url);

    self.movieplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //  self.movieplayer = [MPMoviePlayerController new];
    self.movieplayer.movieSourceType = MPMovieSourceTypeFile;

    NSLog(@"self.movieplayer: %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);

    [self.movieplayer prepareToPlay];

    NSLog(@"self.movieplayer: %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
}

-(void)readytoplay {
    NSLog(@"readytoplay");

    [self.movieplayer.view setFrame:self.view.bounds];  // player's frame must match parent's
    [self.view addSubview:self.movieplayer.view];

    NSLog(@"self.movieplayer: %@", self.movieplayer);
    self.movieplayer.fullscreen = YES;
    NSLog(@"self.movieplayer: %@", self.movieplayer);
    [self.movieplayer play];
}

-(void)mPMoviePlayerLoadStateDidChangeNotification {
    NSLog(@"mPMoviePlayerLoadStateDidChangeNotification %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
}

电影播放器​​属性:

@property (nonatomic, strong) MPMoviePlayerController* movieplayer;
4

2 回答 2

4

首先,您不能在运行时修改(追加、添加、删除)应用程序的包。其次,NSURL对于本地文件,必须使用fileURLWithPath方法进行初始化。尝试纠正此问题,如果无法播放,请尝试观察MPMoviePlayerController's 的loadState变化,当它loadState准备好播放时,再尝试播放文件。希望这可以帮助。

编辑 对不起,我的错,你没有在运行时更改应用程序的包,只是去第二部分。而且,您可以通过调用来获取文件的 url[[NSBundle mainBundle] URLForResource:@"file_name" withExtension:@"extension"];

于 2013-08-27T05:22:20.773 回答
0

你的网址是错误的。就是这样,它没有成功。使用 [NSURL fileURLWithPath:path] 作为 url。它应该工作。

于 2013-08-27T05:56:32.823 回答