无法使用 AVPlayerViewController 显示电影
can't display movies using AVPlayerViewController
我正在尝试在我的视图启动时显示视频。但是我不断让应用程序尝试在目标(我的视图控制器)上呈现一个零模态视图控制器。
这是我的代码。
@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。
我不确定为什么模态视图为零。
谁能帮帮我。
在这里我发现了代码中的错误。您已经创建了 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];
}];
}
我正在尝试在我的视图启动时显示视频。但是我不断让应用程序尝试在目标(我的视图控制器)上呈现一个零模态视图控制器。
这是我的代码。
@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。
我不确定为什么模态视图为零。
谁能帮帮我。
在这里我发现了代码中的错误。您已经创建了 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];
}];
}