我已经完成了大部分工作,但我仍然坚持一些可能很简单的事情。我试图在SpriteKit中播放来自相机胶卷的视频。视频将从主捆绑中播放,视频选择器也可以使用,直到我选择视频。我想我已经锁定主线程,因为在选择视频时,选择器会冻结。非常感谢您的帮助。
·H
#import <SpriteKit/SpriteKit.h>
#import "GameViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
@interface GameScene : SKScene<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
的.m
#import "GameScene.h"
#import <AVFoundation/AVFoundation.h>
@implementation GameScene {
AVPlayer *player;
SKVideoNode *videoNode1;
NSURL *fileURL;
NSString *moviePath;
UIViewController *viewController;
}
-(void)didMoveToView:(SKView *)view {
player = [AVPlayer playerWithURL: fileURL];
videoNode1 = [[SKVideoNode alloc] initWithAVPlayer:player];
videoNode1.size = CGSizeMake(self.frame.size.width,self.frame.size.height);
videoNode1.position = CGPointMake(self.frame.size.width/2.0f,self.frame.size.height/2.0f);
videoNode1.zPosition = 100.0f;
[self addChild:videoNode1];
player.volume = 1.0f;
[videoNode1 play];
}
- (void)getVideo {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
viewController = self.view.window.rootViewController;
[viewController presentViewController: imagePicker animated: YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
fileURL = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
moviePath = [fileURL path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
player = [AVPlayer playerWithURL: fileURL];
}
viewController = self.view.window.rootViewController;
[viewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"place video");
}
答案 0 :(得分:0)
为想要此功能的其他人回答我自己的问题。您必须在imagePicker方法中设置节点和播放器。
- (void)getVideo {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
viewController = self.view.window.rootViewController;
[viewController presentViewController: imagePicker animated: YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
fileURL = [info objectForKey:UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
player = [AVPlayer playerWithURL: fileURL];
videoNode1 = [[SKVideoNode alloc] initWithAVPlayer:player];
videoNode1.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
videoNode1.position = CGPointMake(self.frame.size.width/2.0f, self.frame.size.height/2);
videoNode1.zPosition = 100.0f;
[self addChild:videoNode1];
player.volume = 1.0f;
[videoNode1 play];
}