无法使用 AVPlayerViewController 显示影片



我试图在视图启动时显示视频。但是我一直得到应用程序试图在目标(我的视图控制器)上呈现一个nil模态视图控制器。

这是我的代码。

@property (nonatomic, retain) AVPlayerViewController *moviePlayer;
-(void) viewWillAppear:(BOOL)animated{

NSString *documentsDirectory31 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *documentaryDirectory23 = [documentsDirectory31 stringByAppendingPathComponent:@"ChildOne.txt"];

if([[NSFileManager defaultManager] fileExistsAtPath: documentaryDirectory23]) {
self.user.text =[NSString stringWithContentsOfFile:documentaryDirectory23 encoding:NSUTF8StringEncoding error:NULL];
}
self.sharingtext.hidden = YES;

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];

NSString *audioName = [myDictionary objectForKey:@"photoVideokey"];
NSArray *pathsa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectorya = [pathsa objectAtIndex:0];
NSString *slash = [documentsDirectorya stringByAppendingPathComponent:@"/"];
NSString *documentsDirectory1 = [slash stringByAppendingPathComponent:self.user.text];

//Get a full path to the image in the documents directory.
NSString *fullPath = [documentsDirectory1 stringByAppendingPathComponent:@"/Movie"];


NSString *fullPatha = [fullPath stringByAppendingPathComponent:audioName];
url = [NSURL fileURLWithPath:fullPatha];

NSLog(@"fullPatha:%@",fullPatha);
NSData *videoData = [[NSData alloc] initWithContentsOfURL:url];

AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];


self.sharingImage = [UIImage imageWithData:videoData];
//AVPlayerViewController *playerViewController = [AVPlayerViewController new];

// frame must match parent view
[self.view insertSubview: [moviePlayer view]atIndex:4];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([UIScreen mainScreen].bounds.size.height == 568.0){
moviePlayer.view.frame = CGRectMake(0, 0, 320, 568);
}

else if([UIScreen mainScreen].bounds.size.height == 667.0){
moviePlayer.view.frame = CGRectMake(0, 0, 375, 667);
}

else if([UIScreen mainScreen].bounds.size.height == 736.0){
moviePlayer.view.frame = CGRectMake(0, 0, 414,736);
}
else if([UIScreen mainScreen].bounds.size.height == 812.0){
//11pro76*1.17, 3*1.69 OK!
NSLog(@"THIS IS BEING CALLED 11 pro/12mini/SE2");
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}
else if([UIScreen mainScreen].bounds.size.height == 896.0){
NSLog(@"THIS IS BEING CALLED 11");
//1176*1.29, 3*1.867 OK!
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}
else if([UIScreen mainScreen].bounds.size.height == 844.0){
NSLog(@"THIS IS BEING CALLED 12/pro");
//1176*1.29, 3*1.867 OK!
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}
else if([UIScreen mainScreen].bounds.size.height == 926.0){
NSLog(@"THIS IS BEING CALLED 12PROMAX");
//1176*1.29, 3*1.867 OK!
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}


else {
[[moviePlayer view] setFrame:[self.view bounds]];
}

}

moviePlayer.player = playVideo;
[self presentViewController:moviePlayer animated:YES completion:^{
[self->moviePlayer.player play];
}];
}

我知道文件存在于路径中,因为当我拍摄并保存它时,我可以将其共享到iMovie。
我不知道为什么模态视图是nil。有人能帮我一下吗?

我在这里找到了代码中的bug。你已经创建了AVPlayerController的实例,但你忘了在内存地址。请粘贴下面的代码,你就可以飞行了。

@property (nonatomic, retain) AVPlayerViewController *moviePlayer;

-(void) viewWillAppear:(BOOL)animated{
self.moviePlayer = [[AVPlayerViewController alloc] init];
NSString *documentsDirectory31 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *documentaryDirectory23 = [documentsDirectory31 stringByAppendingPathComponent:@"ChildOne.txt"];

if([[NSFileManager defaultManager] fileExistsAtPath: documentaryDirectory23]) {
self.user.text =[NSString stringWithContentsOfFile:documentaryDirectory23 encoding:NSUTF8StringEncoding error:NULL];
}
self.sharingtext.hidden = YES;

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];

NSString *audioName = [myDictionary objectForKey:@"photoVideokey"];
NSArray *pathsa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectorya = [pathsa objectAtIndex:0];
NSString *slash = [documentsDirectorya stringByAppendingPathComponent:@"/"];
NSString *documentsDirectory1 = [slash stringByAppendingPathComponent:self.user.text];

//Get a full path to the image in the documents directory.
NSString *fullPath = [documentsDirectory1 stringByAppendingPathComponent:@"/Movie"];


NSString *fullPatha = [fullPath stringByAppendingPathComponent:audioName];
url = [NSURL fileURLWithPath:fullPatha];

NSLog(@"fullPatha:%@",fullPatha);
NSData *videoData = [[NSData alloc] initWithContentsOfURL:url];

AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];


self.sharingImage = [UIImage imageWithData:videoData];
//AVPlayerViewController *playerViewController = [AVPlayerViewController new];

// frame must match parent view
[self.view insertSubview: [moviePlayer view]atIndex:4];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([UIScreen mainScreen].bounds.size.height == 568.0){
moviePlayer.view.frame = CGRectMake(0, 0, 320, 568);
}

else if([UIScreen mainScreen].bounds.size.height == 667.0){
moviePlayer.view.frame = CGRectMake(0, 0, 375, 667);
}

else if([UIScreen mainScreen].bounds.size.height == 736.0){
moviePlayer.view.frame = CGRectMake(0, 0, 414,736);
}
else if([UIScreen mainScreen].bounds.size.height == 812.0){
//11pro76*1.17, 3*1.69 OK!
NSLog(@"THIS IS BEING CALLED 11 pro/12mini/SE2");
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}
else if([UIScreen mainScreen].bounds.size.height == 896.0){
NSLog(@"THIS IS BEING CALLED 11");
//1176*1.29, 3*1.867 OK!
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}
else if([UIScreen mainScreen].bounds.size.height == 844.0){
NSLog(@"THIS IS BEING CALLED 12/pro");
//1176*1.29, 3*1.867 OK!
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}
else if([UIScreen mainScreen].bounds.size.height == 926.0){
NSLog(@"THIS IS BEING CALLED 12PROMAX");
//1176*1.29, 3*1.867 OK!
moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
}


else {
[[moviePlayer view] setFrame:[self.view bounds]];
}

}

moviePlayer.player = playVideo;
[self presentViewController:moviePlayer animated:YES completion:^{
[self->moviePlayer.player play];
}];
}

相关内容

  • 没有找到相关文章

最新更新