在 Objective-C 中播放声音之前更改视图

发布于 2024-11-16 08:08:34 字数 1265 浏览 2 评论 0原文

我有一个播放两种声音的程序。我希望用户知道正在播放哪种声音,因此我有两个大按钮图像。我想在三种不同的视图之间进行交换 - 一种视图中两个按钮均为红色(没有播放任何内容),另一种视图中两个按钮之一为绿色(以显示哪个按钮正在播放)。

我遇到的主要问题是我的声音在视图交换之前播放并完成! (我在播放声音之前使用 [window setContentView:firstSound])

更改后的视图最终确实会显示,但只有在播放 wav 的方法完成后才会显示。

我正在使用 NSSound 播放声音文件(它们都是短 wav 文件,所以我认为合适)

有人知道我做错了什么吗?或者如果我有其他方法可以做到这一点?多谢多谢!!

编辑:我已按照建议进行操作,以便仅更改图像(而不是视图),但出现了相同的问题。更新发生在声音播放完毕后。

代码如下:

<代码> -(void)readWavs{

//[window setContentView:firstWav];

[redButton setHidden:YES];

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"fish" ofType:@"wav"];
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
[sound play];
sleep(3); //needed so that the sound will actually play
[sound stop];
[sound release];


//[redButton setHidden:NO];
//[window setContentView:testView];

sleep(2);

//[window setContentView:secondWav];

NSString *resourcePath2 = [[NSBundle mainBundle] pathForResource:@"fish" ofType:@"wav"];
NSSound *sound2 = [[NSSound alloc] initWithContentsOfFile:resourcePath2 byReference:YES];
[sound2 play];
sleep(3);
[sound2 stop];
[sound2 release];

} 绿色的

“打开”按钮应该在声音播放之前显示,但只有在方法完成后才会显示。

I have a program which plays two sounds. I want the user to know which sound is being played, so I have two large images of buttons. I have three different views that I would like to swap between - one where both buttons are red (nothing is playing), and two where one of the two buttons is green (to show which is playing).

The main problem I am getting is that my sound plays and finishes before the view has swapped! (I am using [window setContentView:firstSound] before the sounds are played)

The altered view eventually does show up, but only after the method playing the wavs has finished.

I am playing the sound files using NSSound (they are both short wav files, so I thought it appropriate)

Anyone know what I have done wrong? Or if there is another way I could do this? Thanks heaps!!

Edit: I have followed the advice to make it so that only the image is changed (rather than the view), but the same problem occurs. The update happens after the sounds have finished playing.

Here's the code:


-(void)readWavs{

//[window setContentView:firstWav];

[redButton setHidden:YES];

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"fish" ofType:@"wav"];
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
[sound play];
sleep(3); //needed so that the sound will actually play
[sound stop];
[sound release];


//[redButton setHidden:NO];
//[window setContentView:testView];

sleep(2);

//[window setContentView:secondWav];

NSString *resourcePath2 = [[NSBundle mainBundle] pathForResource:@"fish" ofType:@"wav"];
NSSound *sound2 = [[NSSound alloc] initWithContentsOfFile:resourcePath2 byReference:YES];
[sound2 play];
sleep(3);
[sound2 stop];
[sound2 release];

}

The green 'on' button should show up before the sound plays, but it only does so after the method is done.

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

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

发布评论

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

评论(2

小猫一只 2024-11-23 08:08:34

我不确定我是否理解正确,但是当播放声音并显示正在播放的声音(通过图像)时,您试图切换到完全不同的视图听起来怎么样?如果是这样,您应该只使用 imageView.hidden = YES 和 imageView.hidden = NO 来根据声音显示正确的图像。更好的是,您可以使用一个图像视图并根据声音更改图像视图中显示的图像。

I'm not sure if I'm understanding correctly, but what it sounds like you are trying to switch to a whole different view when a sound is played that displays which sound is being played (by images)? If so, you should just use imageView.hidden = YES and imageView.hidden = NO to display the correct images based on the sound. Better yet, you could use one image view and change the image displayed in the image view based on the sound.

一场春暖 2024-11-23 08:08:34

您正在使用这些 sleep() 阻塞运行循环。在当前事件的所有处理完成之前,显示不会更新,并且在此 readWavs 方法完成之前不会发生这种情况。

如果您想在声音完成后执行某些操作,请为其分配一个委托(可能是启动声音的同一对象)并实现 NSSoundDelegate -sound:didFinishPlaying: 方法。然后,在 readWavs 中,只需启动声音并返回 - 您的委托方法将在完成时被调用,并且因为 readWavs 立即返回,所以它不会阻塞运行循环。

You're blocking the run loop with those sleep()s. The display isn't updated until all the processing for the current event is finished, and that doesn't happen until this readWavs method is finished.

If you want to do something after the sound is finished, assign it a delegate (probably the same object that started the sound) and implement the NSSoundDelegate -sound:didFinishPlaying: method. Then, in readWavs, just start the sound and return - your delegate method will be called when it finishes, and because readWavs returns immediately, it won't block the run loop.

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