我在我的应用中播放随机视频。它们是mp4格式。使用下面的代码,我选择一个随机数,决定播放哪个视频。它似乎播放了很多时间,但有时候只播放没有视频的声音。我测试了z值问题,即使没有背景或任何东西,它仍然会这样做。任何帮助将不胜感激,我尝试编译许多iOS版本,包括9.2,问题仍然存在。不播放视频且只播放声音是随机的,并且没有任何模式(有时很快就会播放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];
答案 0 :(得分:0)
我最好的猜测是这可能会导致你的问题......
node = [self childNodeWithName:@"Goat Hi" ];
if(node)
[node3 removeFromParent];
可能是" Goat Hi"仍然在屏幕上和新创建的视频节点之上。看起来它似乎没有播放,但你仍然会听到声音。
node = [self childNodeWithName:@"Goat Hi" ];
if(node)
[node removeFromParent];
如果是这种情况,应该解决问题。希望这会有所帮助。