简体   繁体   English

Spritekit-有时会播放SKSpriteNode视频,而使用AVPlayer不会播放其他视频

[英]Spritekit - SKSpriteNode video plays sometimes and not others using AVPlayer

I have random videos playing in my app. 我的应用中播放了随机视频。 They are of mp4 format. 它们是mp4格式。 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. 我测试了z值问题,即使没有背景或任何东西,它仍然会这样做。 Any help would be greatly appreciated, I tried compiling in many iOS versions including 9.2, issues still persist. 任何帮助将不胜感激,我尝试在许多iOS版本(包括9.2)中进行编译,问题仍然存在。 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. 不播放视频,只有声音是随机的,没有声音(有时快速播放,有时播放40次或更多),声音会嵌入视频中。

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

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. 屏幕上和新创建的视频节点顶部仍可能存在“ Goat Hi”。 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. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM