Ios 尝试使用MPMoviePlayer时出现黑屏
我使用MPMoviePlayer在我的iPhone应用程序上显示来自外部URL的视频,但是当我运行应用程序时,只会显示一个黑屏。 以下是我正在使用的URL:Ios 尝试使用MPMoviePlayer时出现黑屏,ios,iphone,mpmovieplayercontroller,movie,Ios,Iphone,Mpmovieplayercontroller,Movie,我使用MPMoviePlayer在我的iPhone应用程序上显示来自外部URL的视频,但是当我运行应用程序时,只会显示一个黑屏。 以下是我正在使用的URL: 2015-04-27 00:11:29.655 Floadt[21069:2598414] https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4 以下是我尝试设置MPMoviePlayer的代码
2015-04-27 00:11:29.655 Floadt[21069:2598414] https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4
以下是我尝试设置MPMoviePlayer的代码:
if (entry[@"videos"] != nil) {
NSLog(@"There is a Video: %@", entry[@"videos"]);
NSString *urlString = entry[@"videos"][@"standard_resolution"][@"url"];
NSLog(urlString);
NSURL *url = [NSURL URLWithString:urlString];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player prepareToPlay];
[player.view setFrame: CGRectMake(10, 65, 299, 299)];
[cell.contentView addSubview: player.view];
player.shouldAutoplay = YES;
[player play];
}
您需要将实例保留到MPMoviePlayerController,即作为属性或实例变量。如果您不保留,对电影播放器的引用将丢失。当我们尝试从URL加载视频时,最初它将仅显示空白屏幕。MPMoviePlayerController将花费一些时间从url加载视频。因此,在视频加载之前,我们可以显示视频的第一帧。为此,需要导入两个框架 1.AVF基金会 2.3资产库 使用这两个选项,我们可以将视频的第一帧显示到UIImageView中,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
url=[NSURL URLWithString:@"https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4"];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
{
AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
UIImage *img=[UIImage imageWithCGImage:halfWayImage];
_imgVw.image=img;
}
}
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped)];
[_imgVw addGestureRecognizer:tap];
}
-(void)tapped
{
MPMoviePlayerController *movPlayer=[[MPMoviePlayerController alloc] init];
[movPlayer setContentURL:url];
[movPlayer setMovieSourceType:MPMovieSourceTypeFile];
[movPlayer.view setFrame:CGRectMake(0, 0, _imgVw.frame.size.width, 250)];
[movPlayer prepareToPlay];
movPlayer.controlStyle = MPMovieControlStyleNone;
movPlayer.fullscreen = NO;
movPlayer.shouldAutoplay=YES;
[movPlayer setScalingMode:MPMovieScalingModeAspectFill];
[_imgVw addSubview:movPlayer.view];
[movPlayer play];
}
这里我正在拍摄UIImageView以播放视频。在viewDidLoad中,我正在加载第一帧并向UIImageView提供点击手势。当我点击ImageView时,我正在播放视频