2

我是新来的。我已经 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];

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

非常感谢您的时间。

4

3 回答 3

1

实际上,这是我项目中的工作示例。确保将 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-02T14:49:03.847 回答
0

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

    -(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];
于 2013-05-02T14:05:56.510 回答
0

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>

希望这可以帮助..

于 2015-10-01T13:59:59.523 回答