有聲

2012-07-14 28 views
0

同步動畫我創建圖像陣列和使用動畫它們:有聲

imageview.animationImages = images; 

imageview.animationDuration = 1; 

imageview.animationRepeatCount = 1; 

[imageview startAnimating]; 

這裏imageview是指向對象的UIImageView和images是數組。我正在使用圓形矩形按鈕來觸發動畫。當我在模擬器打開後第一次運行動畫時......動畫不會與聲音同步,但是在我再次按下按鈕之後使用它一次後,它就會執行動作。任何想法爲什麼?我正在使用系統聲音API播放短暫的聲音。我創建一個id的聲音,然後使用播放:

AudioServicesPlaySystemSound(soundID); 

我的猜測是,當我火了,第一次動畫,仿真需要花費一些時間來處理我使用的圖像,並建立數組。我總共使用了45張圖像。因此,聲音保持在動畫圖像之前。但是當我再次嘗試時,圖像已經被處理,所以動畫和聲音都是同步的。但這只是一個猜測。我不知道該怎麼做。如果你想看看整個代碼在這裏,它是:

-(IBAction)btnclicked:(id)sender { 
    NSMutableArray *images = [[NSMutableArray alloc]init]; //all alloc does is allocate memory. The -init method is crucial to actually initialize the object into a working state. 
    for (int imagenumber = 1; imagenumber <= 45; imagenumber++) { 
     NSMutableString *myString = [NSMutableString stringWithFormat:@"%i.png", imagenumber]; 
     [images addObject:[UIImage imageNamed:myString]]; 
    } 
    CGRect frame = CGRectMake(0.0, 0.0, 320, 460); 
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:frame]; 
    imageview.contentMode = UIViewContentModeScaleToFill; 
    imageview.animationImages = images; 

    imageview.animationDuration = 3.2; 

    imageview.animationRepeatCount = 1; 

    [imageview startAnimating]; 

    [self.view addSubview:imageview]; 

    AudioServicesDisposeSystemSoundID(soundID); 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"giggity stick around" ofType:@"wav"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
    [imageview release]; 

} 

只是爲了避免這就是爲什麼我建立時使用迭代任何混亂......我的圖片命名爲1.png,2.png等數組。

+0

https://github.com/kstenerud/ObjectAL-for-iPhone – GTSouza 2012-07-14 04:49:46

+0

可能重複的[UIImageView/AVAudioPlayer同步](http://stackoverflow.com/questions/6199015/uiimageview-avaudioplayer-synchronization) – Abizern 2012-07-14 16:29:59

回答

0

最好的方法是創建一個播放聲音的函數,如下所示: - (void)playSound,然後在你的按鈕動作中你應該寫一個鏈接到playSound函數:[self playSound]。 這裏是一個簡單的:

-(IBAction)XXX:(id)sender 
{ 
    //your imagesArray 
    [self playSound]; 
    imageview.animationDuration = 1; 
    imageview.animationRepeatCount = 1; 
    [imageview startAnimating]; 
} 

-(void)playSound 
{ 
    //yourcode for playing the sound 
} 

希望它的幫助。

編輯: 試試這個:

-(IBAction)btnclicked:(id)sender { 
NSMutableArray *images = [[NSMutableArray alloc]init]; //all alloc does is allocate memory. The -init method is crucial to actually initialize the object into a working state. 
for (int imagenumber = 1; imagenumber <= 45; imagenumber++) { 
    NSMutableString *myString = [NSMutableString stringWithFormat:@"%i.png", imagenumber]; 
    [images addObject:[UIImage imageNamed:myString]]; 
} 
CGRect frame = CGRectMake(0.0, 0.0, 320, 460); 
UIImageView *imageview = [[UIImageView alloc] initWithFrame:frame]; 
imageview.contentMode = UIViewContentModeScaleToFill; 

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/giggity stick around.wav", [[NSBundle mainBundle] resourcePath]]]; 
soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
soundPlayer.volume = 1; 
[soundPlayer play];  

imageview.animationImages = images; 

imageview.animationDuration = 3.2; 

imageview.animationRepeatCount = 1; 

[imageview startAnimating]; 

[self.view addSubview:imageview]; 

} 

我使用AVFoundation播放音頻,現在檢查。

+0

naa it沒有幫助,我編輯了我的問題,看看它。 – 2012-07-14 08:44:01

+0

你能寫出整個代碼嗎? – user1492776 2012-07-14 08:59:30

+0

好吧,我要再次編輯我的問題 – 2012-07-14 09:01:09