【问题标题】:How to play system button click sound on iPad?如何在 iPad 上播放系统按钮点击声音?
【发布时间】:2012-05-24 13:34:14
【问题描述】:

当用户单击我在 iPad 上运行的应用程序中的按钮时,我需要播放一些系统声音。如何在 iOS 中实现?

【问题讨论】:

    标签: objective-c ios xcode ipad


    【解决方案1】:

    如果你想播放一个短的声音(少于 30 秒),你可以像这样轻松地做到这一点:

    注意:您必须添加 AudioToolbox 框架并将其导入 (#import <AudioToolbox/AudioToolbox.h>)

    SystemSoundID mySSID;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"];
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: path], &mySSID); 
    
    AudioServicesPlaySystemSound(mySSID);
    

    还要注意文件可以是:

    • 持续时间不超过 30 秒
    • 线性 PCM 或 IMA4 (IMA/ADPCM) 格式
    • 打包在 .caf、.aif 或 .wav 文件中

    【讨论】:

    • 太棒了!!但是可以播放一些系统声音吗?我不想上传任何新的东西... Somethink 像标准的 iphone 按钮点击声音
    • 我不确定...虽然我确定您可以使用“振动”系统声音。如果我发现了什么,我会告诉你的。
    • 据我所知,您无法访问设备上的系统声音。不过我已经做了一些研究,你可以找到苹果在 iPhone 模拟器中使用的点击声音。 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.sdk/System/Library/Frameworks/UIKit.framework 该文件名为Tock.aiff。只需将此文件添加到您的项目中即可。
    • @LiamGeorgeBetsworth 酷!我猜他可以把文件的副本放到他的包中。
    【解决方案2】:

    你应该使用 AVAudioPlayer。

    有一个很棒的教程here 使用 AVAudioPlayer 播放声音。一个非常简单的使用示例:

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath];
    
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    if (audioPlayer == nil)
        NSLog([error description]);
    else
        [audioPlayer play];
    

    【讨论】:

    • 太棒了!!但是可以播放一些系统声音吗?我不想上传任何新的想法... Somethink 像标准的 iphone 按钮点击声音
    【解决方案3】:
    NSString *path = [[NSBundle mainBundle] pathForResource:@"gorilla 2" ofType:@"mp3"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    
    theAudio.delegate=self;
    [theAudio play];
    

    【讨论】:

      猜你喜欢
      • 2012-10-18
      • 2013-08-29
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多