无法使用 AVPlayerViewController 显示电影
Posted
技术标签:
【中文标题】无法使用 AVPlayerViewController 显示电影【英文标题】:can't display movies using AVPlayerViewController 【发布时间】:2021-02-11 04:56:24 【问题描述】:我正在尝试在我的视图启动时显示视频。但我不断收到应用程序试图在目标(我的视图控制器)上呈现一个 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。 我不确定为什么模态视图为零。 谁能帮帮我。
【问题讨论】:
【参考方案1】:在这里我发现了代码中的错误。您已经创建了 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];
];
【讨论】:
这不起作用。由于已经有一个用于 AVPlayer 的分配器,所以当我分配 moviePlayer 时,我得到了一个 Underfined 符号的错误。但是,如果我注释掉有关呈现视图控制器的部分,我已经消除了错误,但视频没有显示。我将创建一个新帖子来解决这个问题。谢谢 对不起,我的笨蛋,虽然我在 .h 中添加了#import ,但我没有将 AVKit 基础添加到项目中。以上是关于无法使用 AVPlayerViewController 显示电影的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 StorageClass 配置卷 - 无法获取存储帐户的存储密钥
Worklight Studio 和本地开发,有时无法使用 Java 类,有时无法使用 HTML 文件