我的应用中有随机视频播放。它们是 mp4 格式。使用下面的代码,我选择一个随机数来决定播放哪个视频。它似乎经常播放,但其他时候只是播放声音而没有视频。我测试了 z 值问题,即使没有背景或任何东西,它仍然会这样做。任何帮助将不胜感激,我尝试在包括 9.2 在内的许多 iOS 版本中编译,问题仍然存在。不播放视频而只有声音是随机的,并且没有任何模式(有时会很快发生其他时间播放 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];