簡體   English   中英

Spritekit-有時會播放SKSpriteNode視頻,而使用AVPlayer不會播放其他視頻

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

我的應用中播放了隨機視頻。 它們是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];

我最好的猜測是這可能會導致您出現問題...

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

屏幕上和新創建的視頻節點頂部仍可能存在“ Goat Hi”。 使它看起來像沒有播放,但您仍然會聽到聲音。

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

如果是這樣,應該解決該問題。 希望有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM