iOS:如何在特定时间内发生特定操作?

发布于 2024-12-10 10:33:37 字数 593 浏览 1 评论 0原文

在我的应用程序中,我使用 AVAudioPlayer 对象来保存 mp3 文件。我有好几次希望在播放文件时降低文件的音量。

我明白我应该做的一些事情,但我不明白如何定义持续时间以及如何命令某些事情仅在该持续时间内发生。

我正在寻找这样的东西:

AVAudioPlayer * aud1 = [I have a method to get the file];
AVAudioPlayer * aud2 = [I have a method to get the file];
NSTimeInterval * aud1_ti = aud1.duration //I haven't tried this yet - is this the right way to get the mp3 play duration?
[aud1 play];
[aud2 play];
while (aud1_ti)
     aud2.volume = 0.5;
aud2.volume = 1.0

用语言来说 - 我希望在播放 aud1 时,aud2 的音量减半;一旦文件到达末尾,aud2 音量将恢复到最大音量。我无法从文档中理解如何做到这一点。

有人可以帮忙吗?

In my app I'm using AVAudioPlayer objects to hold the mp3 files. I have several occasions where I wish to lower the sound level of the files while they are being played.

I understand some of what I'm supposed to be doing, but I don't understand how I define the duration and how do I command that something will happen only for that duration.

I'm looking for something like this:

AVAudioPlayer * aud1 = [I have a method to get the file];
AVAudioPlayer * aud2 = [I have a method to get the file];
NSTimeInterval * aud1_ti = aud1.duration //I haven't tried this yet - is this the right way to get the mp3 play duration?
[aud1 play];
[aud2 play];
while (aud1_ti)
     aud2.volume = 0.5;
aud2.volume = 1.0

To say it in words - I wish that while aud1 is playing, aud2 volume will be reduced in half; and once the file had reached its end the aud2 volume will get back to its full level. I couldn't understand from the documentation how to do that.

Can anyone assist?

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

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

发布评论

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

评论(1

鱼窥荷 2024-12-17 10:33:37

您可能想要做的不是让 AVAudioPlayer 的代表响应 – audioPlayerDidFinishPlaying:successively: 并重置当时的音量。在非常非常基本的层面上,您将得到如下内容:

- (void)startPlaying {
     self.aud1.delegate = self;
     [self.aud1 play];

     self.aud2.volume = 0.5;
     [self.aud2 play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) {
     if (self.aud2) {
          self.aud2.volume = 1.0;
     }
}

注意,上面的代码显然不完整,但应该为您指明正确的方向。

Rather than basing this off of the duration what you probably want to do is have the delegate of your AVAudioPlayer respond to – audioPlayerDidFinishPlaying:successfully: and reset the volume at that time. On a very, very basic level you will have something like this:

- (void)startPlaying {
     self.aud1.delegate = self;
     [self.aud1 play];

     self.aud2.volume = 0.5;
     [self.aud2 play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) {
     if (self.aud2) {
          self.aud2.volume = 1.0;
     }
}

Note, the code above is obviously incomplete but should point you in the right direction.

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