【问题标题】:How to manage UIButton click sound iOS如何管理 UIButton 点击​​声音 iOS
【发布时间】:2014-01-19 00:50:41
【问题描述】:

我正在使用以下代码在按钮单击时获得声音。它完美无缺。问题是如果我降低手机的音量,这个声音并没有减少。我想根据音量按钮管理按钮点击声音。我没有使用输入视图。

SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((CFURLRef) CFBridgingRetain(soundUrl) , &soundID);
AudioServicesPlaySystemSound(soundID);

【问题讨论】:

  • 上面的代码写在按钮点击方法里面

标签: ios audio ios7 uibutton


【解决方案1】:

检查Click here,这可能会对您有所帮助。

【讨论】:

    【解决方案2】:

    我所做的是为我的按钮声音创建一个 AVAudioPlayer 并播放它。 AVAudioPlayer 尊重系统音量。

    我创建了一个单独的 IBAction 方法来播放声音,然后我将我的按钮链接到这两个动作。这样,只需链接动作即可轻松打开和关闭声音。如果您总是希望播放声音,那么您可能希望直接在您的 IBAction 方法中调用声音播放方法。

    【讨论】:

      【解决方案3】:

      按照 Duncan C 的建议,试试 AVAudioPlayer:

      1. 导入AVFoundation.framework

      2. 将您的代码 sn-p 替换为:

        NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
        NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
        
        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
        [player play];
        

      【讨论】:

      • 我试过这个,但是得到一个错误 No visible @ interface for AVAudioplayer 声明了选择器 initwithurl。
      • 我清除了这个错误,但没有声音播放。它不是 initwithurl,它是 initwithcontentsofurl。
      • @suraj 你能告诉我你是如何消除这个错误的 - AVAudioplayer 没有可见的@接口声明选择器 initwithurl。
      • 抱歉回复/更正晚了,暂时离线。 Suraj,你还是听不到声音吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多