gpt4 book ai didi

ios - 无法使用 MPMoviePlayerController 播放视频文件

转载 作者:行者123 更新时间:2023-11-28 19:08:07 25 4
gpt4 key购买 nike

视频文件是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;

最佳答案

首先,您不能在运行时修改(追加、添加、删除)应用程序的包。其次,本地文件的 NSURL 必须用 fileURLWithPath 方法初始化。尝试纠正这个问题,如果它不播放,请尝试观察 MPMoviePlayerControllerloadState 变化,以及它的 loadState 何时准备就绪播放,然后尝试播放该文件。希望这会有所帮助。

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

来获取文件的 url

关于ios - 无法使用 MPMoviePlayerController 播放视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18457203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com