Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone ios MPMoviePlayerController可以';t在模拟器上播放本地文件_Iphone_Ios_Xcode_Mpmovieplayercontroller_Thumbnails - Fatal编程技术网

Iphone ios MPMoviePlayerController可以';t在模拟器上播放本地文件

Iphone ios MPMoviePlayerController可以';t在模拟器上播放本地文件,iphone,ios,xcode,mpmovieplayercontroller,thumbnails,Iphone,Ios,Xcode,Mpmovieplayercontroller,Thumbnails,我是新来的。我已经花了两天的时间来寻找解决方案,但并不开心 这是我的密码: NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *docaPathFull = [docPath stringByAppendingPathComponent:@"/IMG_0003.m4v"]; self.vid

我是新来的。我已经花了两天的时间来寻找解决方案,但并不开心

这是我的密码:

NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:@"/IMG_0003.m4v"];
self.videoURL = [NSURL fileURLWithPath:docaPathFull];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage  *thumbnail = [player thumbnailImageAtTime:4.0 timeOption:MPMovieTimeOptionExact];
player = nil;       
self.imageView.image = thumbnail;
问题是,我无法使MPMoviePlayerController的视频文件可见。我无法得到缩略图,也无法播放,只有黑色播放器和加载无限

我确实试过:

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"IMG_0003" ofType:@"m4v"];
self.videoURLtheurl=[NSURL fileURLWithPath:thePath];
还有这个

self.videoURL = [[NSBundle mainBundle] URLForResource:@"IMG_0005" withExtension:@"mov"];
NSString *filePath = @"/Users/[user]/Documents/video/IMG_0004.mov";
self.videoURL = [NSURL fileURLWithPath:filePath];
还有这个

self.videoURL = [[NSBundle mainBundle] URLForResource:@"IMG_0005" withExtension:@"mov"];
NSString *filePath = @"/Users/[user]/Documents/video/IMG_0004.mov";
self.videoURL = [NSURL fileURLWithPath:filePath];
如果你有任何想法如何拯救我的生命,请做


非常感谢您抽出时间。

您可以使用以下方法获取给定视频Url的缩略图

    -(UIImage *) giveThumbofVideoUrl:(NSURL *) url{
            NSString *urlString = [url absoluteString];
            AVAsset *asset = [AVAsset assetWithURL:url];
            AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
            imageGenerator.appliesPreferredTrackTransform = YES;
            //imageGenerator.maximumSize = CGSizeMake(320,480);
            CMTime time = CMTimeMake(1, 1);
            CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
            UIImage *image = [UIImage imageWithCGImage:imageRef];
            CGImageRelease(imageRef);
    return image;
   }
如何使用它?,

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"IMG_0003" ofType:@"m4v"];
NSURL *movieUrl=[NSURL fileURLWithPath:thePath];
UIImage  *thumbnail = [self giveThumbofVideoUrl:movieUrl];

实际上,这是我在项目中的工作示例。确保已将m4v文件添加到项目中。这个通知是我需要的,也许你不需要

theMovie = [[MPMoviePlayerController alloc] initWithContentURL:
    [NSURL fileURLWithPath: [[NSBundle mainBundle] 
    pathForResource:@"yourmovie" ofType:@"m4v"]]];

theMovie.view.frame = CGRectMake(0, 0, 320, 480);
[theMovie setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:theMovie];
[self.view addSubview:theMovie.view];    
[theMovie play];

MPMoviePlayerController
现在不推荐使用。所以我使用了
avplayervewcontroller
。并编写了以下代码:

NSURL *videoURL = [NSURL fileURLWithPath:filePath];
    //filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    //[playerViewController.player play];//Used to Play On start
    [self presentViewController:playerViewController animated:YES completion:nil];
请不要忘记导入以下框架:

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#导入
#进口

希望这有帮助。

视频的确切路径是什么?它是在文档目录中还是在应用程序包中?很感谢您的快速响应,但我需要将此文件播放到。所以MPMoviePlayerController将是完美的解决方案。我可以从http播放文件,但localy不能。请确保全局定义moviePlayer变量@property(非原子,强)MPMoviePlayerController*player;另外,您应该将moviePlayer.view作为子视图添加到self.viewfrom http url播放器工作正常,但在我将url从http更改为local时不工作。谢谢。项目文件中的位置不同?以下是您的代码生成的错误:2013-05-02 16:15:13.592 iphone[15317:18e03]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'***-[NSURL initFileURLWithPath:]:nil string参数'这只是一个猜测,但如果您的NSBundle mainBundle返回nil,可能是因为您创建了命令行工具类型的项目。。。请参见:或者该文件不是项目的一部分。您可以通过访问项目的构建阶段/复制捆绑资源来再次检查这一点。你应该在那里找到你的电影文件。右键单击它并选中“在查找器中显示”和/或“在项目导航器中显示”。它应该出现在两个地方。。希望它能帮助你!