UIImageView/AVAudioPlayer 同步

发布于 2024-11-11 14:22:37 字数 996 浏览 4 评论 0原文

我有一个 UIView,它拥有一个 UIImageView 作为子视图。该图像视图旨在显示动画(基本上,使用 startAnimating 方法)。当动画开始时,我还需要播放声音。为此,我使用 AVAudioPlayer 的prepareToPlay 和play 方法。

我遇到的问题是,第一次启动全局动画(图像动画本身+声音)时,在图像动画实际启动之前系统地播放声音。考虑到没有任何同步,这一点也不奇怪。

但如何实现这种同步呢?是否有某种回调可以用来知道图像动画何时播放并从那里启动声音播放...... 或者也许耦合 UIImageView 和 AVAudioPlayer 根本不是一个好主意?

这是我当前的实现:

- (void)playSample {
    previewView_ = [[[PreviewView alloc] initWithFrame:topView.bounds
                                      backgroundImages:backgroundAnimationImages 
                                       characterImages:characterAnimationImages] autorelease];

    [previewView_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];


    [topView addSubview:previewView_];

    [audioPlayer_ setDelegate:self];
    [audioPlayer_ setVolume:1.0];   

    [previewView_ startPreview]; // This calls startAnimating on the UIImageView of previewView_
    [audioPlayer_ playSound];

}

I have an UIView that possess an UIImageView as a subview. This image view is intended to display an animation (basically, with the startAnimating method). When the animation start, I also need to play a sound. For this purpose, I use AVAudioPlayer's prepareToPlay and play methods.

Problem I encounter is that the FIRST TIME the global animation (image animation itself + sound) is launched, the sound is systematically played before the image animation is actually started. Not weird at all considering there is no synchronisation whatsoever.

But how could this synchronization be achieved? Is there some sort of callback which could be used know when the image animation is playing and launch the sound play from there...
Or maybe coupling UIImageView and AVAudioPlayer is not a good idea at all?

Here is my current implementation :

- (void)playSample {
    previewView_ = [[[PreviewView alloc] initWithFrame:topView.bounds
                                      backgroundImages:backgroundAnimationImages 
                                       characterImages:characterAnimationImages] autorelease];

    [previewView_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];


    [topView addSubview:previewView_];

    [audioPlayer_ setDelegate:self];
    [audioPlayer_ setVolume:1.0];   

    [previewView_ startPreview]; // This calls startAnimating on the UIImageView of previewView_
    [audioPlayer_ playSound];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-11-18 14:22:37

也许你可以使用:

[音频播放器播放时间:[音频播放器
deviceCurrentTime] + someDelayTimeInterval]

我发现audioPlayer(或prepareToPlay)搞乱了我的显示更新,所以我最终做的是为viewDidLoad中的每个声音创建audioPlayer:并在后台线程中调用的方法中播放它:

[self PerformSelectorInBackground:@selector(playAudioPlayer:)
withObject:self.audioPlayer];

Maybe you could use:

[audioPlayer playAtTime:[audioPlayer
deviceCurrentTime] + someDelayTimeInterval]

I found the the audioPlayer (or prepareToPlay) was messing up with my display updates, so what I ended up doing was to create the audioPlayer for each sound in viewDidLoad: and play it in a method called in a background thread:

[self performSelectorInBackground:@selector(playAudioPlayer:)
withObject:self.audioPlayer];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文