使动画与声音同步



我创建了一个图像阵列,并使用为它们设置动画

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

最好的方法是创建一个播放声音的函数,比如:-(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来播放音频,现在就检查一下。

最新更新