【问题标题】:Playing multiple audio files for through multiple buttons通过多个按钮播放多个音频文件
【发布时间】:2014-03-05 16:48:26
【问题描述】:

我正在尝试使用音板应用程序,并且我的视图控制器上需要超过 50 个按钮(每个声音一个按钮)。此外,我想完全以编程方式制作按钮(和音频播放器代码),因为在故事板和 .h/.m 文件之间不断切换有点烦人。我没有复制和粘贴相同的按钮代码 50 次,而是使用这个 for 循环为我制作按钮:

NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=100;
int buttonHeight=50;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame     = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
    [aButton addTarget:self action:@selector(playAudioMethod) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:aButton];

    yCoord += buttonHeight + buffer;
}
[scrollView setContentSize:CGSizeMake(700, yCoord)];

按钮单击事件选择器中的 playAudioMethod 只播放一种声音。怎么会有50个声音,每个对应一个单独的按钮?抱歉,如果这是一个非常基本的问题,我仍在学习目标 C。谢谢!

编辑:

这是 playAudioMethod:

- (void) playButtonSound:(id)inSender
{
    AudioServicesPlaySystemSound(self.SoundID);
}

它基本上只是播放 SoundID 属性,我通过这个传递了一个声音文件:

   NSURL *buttonURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"medic_taunts01" ofType:@"wav"]];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
    self.SoundID = SoundID;

【问题讨论】:

  • 显示你的 playAudioMethod 方法
  • 完成,我刚刚添加了播放音频方法。

标签: ios objective-c audio


【解决方案1】:

playAudioMethod 有一个参数,即sendersender 是生成消息的按钮。

- (void)playAudioMethod:(id)sender
{
    UIButton *button = (UIButton *)sender;
    switch (button.tag)
    {
           //....
    }
}

您可以使用按钮的tag 属性来存储一个值,例如,您可以在playAudioMethod 中存储一个允许您识别按钮的整数,或者您可以直接存储音频资源的名称。

【讨论】:

  • 我会试试的,但我可以在 for 循环中增加标签吗?也许使用类似: [button tag++] ?谢谢。
  • 你宁愿做类似button.tag = i
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多