2009-12-10 59 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