Reputation: 330
I am attempting to display a video using Sprite Kit and SKVideoNode on iOS 7.1. Apple's documentation makes this seem pretty straight forward. An example taken from the Sprite Kit Programming Guide:
SKVideoNode *sample = [SKVideoNode videoNodeWithVideoFileNamed:@"sample.m4v"];
sample.position = CGPointMake(CGRectGetMidX(self.frame),
[self addChild: sample];
[sample play];
I am trying the same code and seeing no video. The SKVideoNode is indeed in the node hierarchy. I've also tried multiple videos, all are H.264 AAC.
I found someone with a similar issue here. They seemed to solve it by instantiating an AVPlayer. That doesn't display video for me either. Ideas?
Upvotes: 2
Views: 2893
Reputation: 67
You should either add the video file to your bundle and load it through your bundle, or deactivate sandboxing.
Upvotes: -1
Reputation: 5967
I know the question is asked around 20 months ago, but never the less I thought I should share my learnings as well (it could be helpful to programmers who are working on SKVideoNode
and facing the same issue, also there case is same as mine and mentioned below), related to SKVideoNode
, video view display related issues.
I was recently exploring Sprite Kit
where I found SKVideoNode
to play video files on a SKScene
. I created a GameScene
inherited from SKScene
and added an instance of UIImageView
as backgroundImage
on scenes skview
([self.scene.view addSubview:aView]
) and next I created a SKVideNode's
instance along with other UIKit
subviews and SKNode
childs added to the scene.view
and scene
respectively.
I configured the code for SKVideNode's
as is mentioned in its documents of class refrence, I build and run the application to see video playing. But apart from video everything appeared on the screen, also when I increase the volume of the device, I was able to hear the sound(audio) of the video file.
Since I was able to hear audio so I thought I am missing some configuration or their is some issue due to which video view is not visible.
I tried some permutations and combination to use AVPlayer
, different file formats but still the same behavior after this I started digging the views layering
and z index
concept to see if the video is not behind the background image or any other view, still same behavior.
Finally, what I did I removed every child
and subviews
(including background image) from the screen and tried to just play video, and this time the video view was visible on screen.
In short what I concluded with above behaviors is that, "if using Sprite Kit's
SKVideoNode
want to play videos than we should play them in SKScene
instances, and not on above any UIKit
views, as the instance of SKVideoNode
is not visible if we are trying to add it at position where there is a UIKit
view already present".
Well, as far as SpriteKit
is concerned, it has SKView
and SKScene
inside a scene which allows a programmer to use both SpriteKit
and UIKit
elements in there games, but these types of issues could lead to a compromise in selecting the type of visual elements we want to use.
Upvotes: 1
Reputation: 86
I had just solved it by converting the video file's format with Prism, MPEG4 video encoder. Hope it helps.
Upvotes: 2
Reputation: 11696
Couple of things first:
Try out the sample code I have below:
#import "MyScene.h"
#import <AVFoundation/AVFoundation.h>
@implementation MyScene
{
AVPlayer *_player;
SKVideoNode *_videoNode;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
[self setupMovie];
}
return self;
}
-(void)setupMovie
{
NSURL *fileURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"loop" ofType:@"mov"]];
_player = [AVPlayer playerWithURL: fileURL];
_videoNode = [[SKVideoNode alloc] initWithAVPlayer:_player];
_videoNode.size = CGSizeMake(200, 100);
_videoNode.position = CGPointMake(150, 150);
[self addChild:_videoNode];
_player.volume = 1.0;
[_videoNode play];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//
}
-(void)update:(CFTimeInterval)currentTime
{
//
}
@end
Upvotes: 4