1

I have random videos playing in my app. They are of mp4 format. Using code below I select a random number which decides which video to play. It seems to play a lot of the time but other times just the sound plays with no video. I tested z value issues and it still does this even when there is no background or anything. Any help would be greatly appreciated, I tried compiling in many iOS versions including 9.2, issues still persist. The not playing video and only sound is random and no patterns to them(sometimes quickly happens other times 40 plays or more), sound is embedded in the video.

-(SKVideoNode*)prepareHiVideo:(SKVideoNode*)videoNode withFileURL: 
(NSURL*)fileURL
{
//AVPlayer
AVPlayer * avPlayer = [AVPlayer playerWithURL:fileURL];
[[NSNotificationCenter defaultCenter]addObserver:self  
selector:@selector(hiFinishedPlaying:) 
name:AVPlayerItemDidPlayToEndTimeNotification object:[avPlayer 
currentItem]];


//SKVideo Node
videoNode = [SKVideoNode videoNodeWithAVPlayer:avPlayer];
videoNode.zPosition=4;
[videoNode setScale:.338];
return videoNode;
} 
-(void)animatedHis {
SKVideoNode * node;
name.text = @"";
NSURL *fileURL;
AVPlayer * hiPlayer = [[AVPlayer alloc]init];
randomNumber = arc4random_uniform(9);
while (randomNumber==previousRandom)
{
randomNumber = arc4random_uniform(9);
}
if(randomNumber==0)
{

fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"GrizzlyBearHi" ofType:@"mp4"]];
node=[self prepareHiVideo:node withFileURL:fileURL];
node.name=@"Grizzly Bear Hi";
node.position=CGPointMake(self.frame.size.width/5.6, self.frame.size.height/1.17);
name=[SKLabelNode labelNodeWithFontNamed:@"Helvetica-Bold"];
name.zPosition=10;
name.fontColor = [UIColor whiteColor];
name.text=@"Grizzly Bear";
name.position=CGPointMake(self.frame.size.width/5.6, self.frame.size.height/1.35);


}

First of the 9 random numbers code above, I have all 9 numbers and
ifs in code.

After ifs: 
node.zPosition=10;
name.fontSize = 18;
[self addChild:name];
[self addChild:node];
[node play];
previousRandom=randomNumber;


Cleanup code after video completes:
-(void)hiFinishedPlaying:(NSNotification *)notification
{
SKNode * node;


node = [self childNodeWithName:@"Grizzly Bear Hi" ];
if(node)
[node removeFromParent];

node = [self childNodeWithName:@"Beaver Hi" ];
if(node)
 [node removeFromParent];

 node = [self childNodeWithName:@"Goat Hi" ];
 if(node)
 [node3 removeFromParent];


node = [self childNodeWithName:@"Mountain Lion Hi" ];
 if(node)
 [node removeFromParent];

node = [self childNodeWithName:@"Bald Eagle Hi" ];
 if(node)
 [node removeFromParent];

node = [self childNodeWithName:@"Squirrel Hi" ];
 if(node)
 [node removeFromParent];

node = [self childNodeWithName:@"Wolf Hi" ];
if(node)
 [node removeFromParent];

node= [self childNodeWithName:@"Coyote Hi"];
if(node)
 [node removeFromParent];

node= [self childNodeWithName:@"Rabbit Hi"];
(if(node)
 [node removeFromParent];
Eric Means
  • 123
  • 1
  • 10

1 Answers1

0

My best guess is this might be causing you issues...

 node = [self childNodeWithName:@"Goat Hi" ];
 if(node)
 [node3 removeFromParent];

it could be that "Goat Hi" is still there on screen and on top of your newly created video node. Making it look like it isn't playing but you would still get sounds.

 node = [self childNodeWithName:@"Goat Hi" ];
 if(node)
 [node removeFromParent];

Should fix the problem if that is the case. Hopefully that helps.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30
  • Thanks I should have removed the 3. I had each node named node1, node2, but changed them all to node just for stack overview. It isn't the issue. I think it may have to do with other actions occurring on the same screen as the video playing but it's just a theory – Eric Means Mar 31 '16 at 05:24
  • May need a seperate thread from main to play video but just a thought. – Eric Means Mar 31 '16 at 05:25
  • Ended up being an apple iOS < 10 bug with videos in SpriteKit. iOS 10 works fine. – Eric Means Oct 04 '16 at 16:42