【问题标题】:iOS - Playing Tock sound for custom keyboardiOS - 为自定义键盘播放 Tock 声音
【发布时间】:2015-07-09 01:16:44
【问题描述】:

我正在尝试为我的自定义键盘播放 Tock 声音。

我尝试过的:

[[UIDevice currentDevice] playInputClick];

NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);

AudioServicesPlaySystemSound(0x450);

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = 1;
[myAudioPlayer play];

这给了我一个错误:

AudioSessionClient: Couldn't connect to com.apple.audio.AudioSession
Couldn't connect to com.apple.audio.AudioQueueServer; AudioQueue will not be usable

并且还研究了这个link

当我在我的 ViewControllers(不是自定义键盘)中尝试这些代码时,一切都奏效了。但是当我在我的自定义键盘上测试它时,它至少闲置了 10 秒,我什么也听不见。

我还确保我的 iPhone 未处于静音模式,扬声器未损坏,键盘点击已启用。我在 iPhone 和 iPad 上测试了这些代码,结果是一样的

Xcode 版本 6.3.1 iOS 8.3 版

我做错了什么?提前致谢。

【问题讨论】:

    标签: ios objective-c iphone xcode keyboard


    【解决方案1】:

    我找到了解决问题的方法。

    在 Info.plist 中。

    RequestsOpenAccess = YES
    

    并在我的KeyboardViewController.m 上使用此代码

    dispatch_async(dispatch_get_main_queue(), ^{
            AudioServicesPlaySystemSound(1104);
        });
    

    【讨论】:

      【解决方案2】:

      您必须在您的输入视图上实现<UIInputViewAudioFeedback> 协议。 IE。你有一个键盘(UIView 子类),上面有按钮,它实现了这个代码: - .h文件:

      @interface RHTableKeyboard : UIView <UIInputViewAudioFeedback>
      
      @end
      

      .m文件:

      - (BOOL)enableInputClicksWhenVisible
      {
          return YES;
      }
      

      然后你可以继承 UIButton 并做这样的事情:

      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [super touchesBegan:touches withEvent:event];
          if (highlightColour) {
              origionalColour = self.backgroundColor;
              [self.layer setBackgroundColor:[highlightColour CGColor]];
          }
          if (makeClickSound) {
              [[UIDevice currentDevice] playInputClick];
          }
      }
      
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [super touchesEnded:touches withEvent:event];
          if (highlightColour) {
              [self.layer setBackgroundColor:[origionalColour CGColor]];
          }
      }
      

      (作为奖励,我输入了代码以使其改变颜色。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多