1

我已经完成了大部分工作,但我遇到了一些可能很简单的事情。我试图在 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");
 }
4

1 回答 1

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];

 }
于 2016-02-23T23:01:26.593 回答