【问题标题】:"Plays Sound" Property of UIButton in Interface BuilderInterface Builder 中 UIButton 的“播放声音”属性
【发布时间】:2015-04-13 17:49:40
【问题描述】:

在界面生成器中,可访问性下的 UIButton 属性显示“播放声音”。

谁能解释一下这是什么。实际上,我正在制作一个应用程序,该应用程序在每次单击按钮时都会播放声音,并且我可以从设置屏幕中禁用声音。 UIButton 的这个属性能帮到我吗?

谢谢

【问题讨论】:

  • 如果您只需要按键点击时的键盘点击声音,您可以使用[[UIDevice currentDevice] playInputClick];
  • 是的,我只需要播放点击声。我在按钮操作中添加了这一行并在模拟器中进行了测试,但没有播放声音。

标签: iphone objective-c ios interface-builder


【解决方案1】:

您可以使用[[UIDevice currentDevice] playInputClick]; 播放UIDevice 中提供的键盘输入点击声音。检查这个apple documentation for more details.

为此,您需要执行以下操作,

  1. 在您的输入视图类中采用UIInputViewAudioFeedback 协议。
  2. 实现enableInputClicksWhenVisible委托方法返回YES。

UIView 类中执行此操作,

@interface MyView : UIView <UIInputViewAudioFeedback>

然后实现enableInputClicksWhenVisible方法

- (BOOL)enableInputClicksWhenVisible
{
    return YES;
}

如果您遇到此问题,可以check this.

【讨论】:

【解决方案2】:

您是否可以使用 Plays Sound 的“属性”(它实际上可能不是一个属性)来使您的按钮播放声音的原始问题的答案是:不。Plays Sound 是一个“特征”,它描述了使用 VoiceOver 的人(即最有可能是盲人)的对象(在本例中为按钮)。你可以阅读文档here

【讨论】:

    【解决方案3】:

    嘿,您想在按钮单击事件上播放声音,然后也使用下面的代码..

    -(IBAction)playSound:(id)sender
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"adriantnt_release_click" ofType:@"mp3"]; /// set .mp3 name which you have in project
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        theAudio.delegate=self;
        [theAudio play];
    } 
    

    并像下面这样使用..

    -(IBAction)yourButton_Clicked:(id)sender
    {
        [self performSelector:@selector(playSound:) withObject:sender]; 
        //your code  write here
    }
    

    注意: 添加AudioToolbox.framework Framework 并同时导入 #import&lt;AVFoundation/AVAudioPlayer.h&gt;.h 文件中,并在.h 文件中添加代表AVAudioPlayerDelegate

    希望对你有帮助……

    【讨论】:

    • 目前我正在使用这种方法,它工作正常。但我需要缩短我的代码。我可以使用 UIButton 的 Plays Sound 属性来播放声音吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多