2009-12-10 61 views
0

基本上,每當我播放聲音動畫放緩或暫停,該怎麼辦?幫助!當播放聲音動畫凍結暫停

聲音大小20kb mp3。

動畫尺寸5kb png。

代碼:

- (void) startAnimation { 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.8]; 
CGPoint destination = CGPointMake(152,20); 
pink.center = destination; 
[UIView commitAnimations]; 
[soundPink play];  
} 


- (void) initSoundResources { 

soundPink = [[APlayer alloc] initWithFile:@"pink" type:@"mp3"]; 
soundPink = 0.4; 
} 

回答

2

編輯:

我剛剛注意到,在initSoundResources,你alloc'ing和init'ing的APlayer對象,但隨後分配浮到指針直線後。我很確定這不是你想要的。


試試這個:

- (void) startAnimation { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.8]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(playSound)]; 
    CGPoint destination = CGPointMake(152,20); 
    pink.center = destination; 
    [UIView commitAnimations]; 
} 


- (void) initSoundResources { 
    soundPink = [[APlayer alloc] initWithFile:@"pink" type:@"mp3"]; 
    soundPink = 0.4; 
} 


- (void) playSound { 
    //assuming initSoundResources is called at some point before this... 
    [soundPink play]; 
} 

這裏的想法是,UIView的動畫是異步的。如果動畫長度設置爲0.8秒,那並不意味着代碼需要0.8秒才能運行。

我的意思是直接在UIView動畫代碼後面的代碼在動畫代碼後立即被有效地調用。在這種情況下,聲音即將播放(可能)在動畫開始之前。

我無法想象爲什麼它會暫停或凍結,但使用這種方法應該工作。

在我的代碼中,我已經假設initSoundResources方法在嘗試播放聲音之前的某個時刻被調用。

如果soundPink指針未初始化爲nil或有效的聲音實例,則向其發送播放消息可能會導致崩潰。

+0

首先感謝! 我是否需要更改initSoundResources方法中的某些內容? 我只是綜合soundPink,我需要做更多的事嗎? 如何將[UIViewsetAnimationDidStopSelector:@selector(playSound)]; 如果我需要定時器的聲音? [NSTimer scheduledTimerWithTimeInterval:1.8 target:self selector:@selector(playSound)userInfo:nil repeats:false]; – nil 2009-12-10 14:19:37

+0

我給你的代碼應該工作,關於UIViewAnimation委託和didStopSelector。 關於你的initSoundResources,你不需要指定一個float到soundPink指針。編譯器當然應該警告你,你沒有看到它嗎? – Jasarien 2009-12-10 17:55:55

+0

我沒有看到任何警告,你是什麼意思分配一個浮動? – nil 2009-12-10 21:55:41