Play video from camera roll in SpriteKit SKVideoNode

294 views Asked by At

I've got most of this working but I'm stuck with something that might be simple. Im trying to play a video from the camera roll in SpriteKit. A video will play from the main bundle, and the video picker also works until I choose the video. I think I'm locking the main thread because when choosing the video the picker freezes. You're help is extremely appreciated.

.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");
 }
1

There are 1 answers

0
Jeff On

Answering my own question for others who want this feature. You must set up your node and player in the imagePicker method.

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

 }