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 等等,这就是我在构建数组时使用迭代的原因。

4

1 回答 1

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 播放音频,现在检查一下。

于 2012-07-14T06:05:20.327 回答