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