【问题标题】:ios MPMoviePlayerController can't play local files on simulatorios MPMoviePlayerController 无法在模拟器上播放本地文件
【发布时间】:2013-04-26 17:48:35
【问题描述】:

我是新来的。我已经浏览了 2 天的解决方案,但不满意。

这是我的代码:

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

如果你有任何想法如何拯救我的生命,请做!!!

非常感谢您的宝贵时间。

【问题讨论】:

  • 视频的具体路径是什么?它是在文档目录中还是在应用程序包中? 困惑

标签: iphone ios xcode mpmovieplayercontroller thumbnails


【解决方案1】:

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

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

【讨论】:

  • 感谢您的快速响应,但我需要播放此文件。所以 MPMoviePlayerController 将是完美的解决方案。我可以从 http 播放文件,但本地不行。
  • 确保全局定义moviePlayer变量,@property(nonatomic, strong) MPMoviePlayerController *player;你也应该将moviePlayer.view作为子视图添加到self.view
  • 来自 http url 播放器工作正常,但不工作,当我将 url 从 http 更改为本地时。
【解决方案2】:

实际上,这是我项目中的工作示例。确保将 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];

【讨论】:

  • 谢谢。项目文件的位置不同吗?
  • 这是您的代码生成的错误:2013-05-02 16:15:13.592 iphone[15317:18e03] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* ** -[NSURL initFileURLWithPath:]: 无字符串参数'
  • 这只是一个猜测,但如果您的 NSBundle mainBundle 返回 nil,那可能是因为您创建了一个命令行工具类型的项目...请参阅:stackoverflow.com/questions/3949368/…
  • 或者只是文件不是您项目的一部分。您可以通过访问项目的构建阶段/复制捆绑资源来仔细检查这一点。您应该在那里找到您的电影文件。右键单击它并选中 Show in Finder 和/或 Reveal in Project Navigator。它应该出现在两个地方..希望对您有所帮助!
【解决方案3】:

MPMoviePlayerController 现已弃用。所以我使用了AVPlayerViewController。并编写了以下代码:

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>

希望这有帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多