In everyone’s rush to make the next Candy Crush one of the more interesting features of iOS7’s new SpriteKit framework has flown under the radar. The SKVideoNode suddenly makes it super easy to play multiple videos simultaneously, something that was a little too tedious with the AVFoundation framework (see my previous blog post on this), and is not possible with the standard Media Player Framework. Now with three lines of code, we can create an SKVideoNode object, add it to the scene and play it. SpriteKit doesn’t expose a lot for controlling the video beyond play and pause, if you want lower level control you can reference your own AVPlayer, but the SKVideoNode is a fully fledged sprite that can have effects and actions applied to it. I’ve done a little bit of performance testing, playing 4 videos at once is no real issue for the new generation iPhones (5+).
In the code below we simply instantiate a new video node instance, position it, add it to the scene, play it, and repeat. So easy! For this example I exported two short landscape videos from After Effects at 320×180 pix in mp4 codec.
The simplicity of the SkVideoNode, and it’s inclusion in the SpriteKit framework, opens up a multiplicity of possibilities for video, interactivity and effects. I’m oddly excited about it and I hope Apple expands it’s capabilities in future releases.
-(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { SKVideoNode *vid1 = [SKVideoNode videoNodeWithVideoFileNamed:@"myVideo1.mp4"]; vid1.position = CGPointMake(160., 180); [self addChild:vid1]; [vid1 play]; SKVideoNode *vid2 = [SKVideoNode videoNodeWithVideoFileNamed:@"myVideo2.mp4"]; vid2.position = CGPointMake(160., 400); [self addChild:vid2]; [vid2 play]; } return self; }