Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在没有振动的情况下在iOS上播放系统声音?_Iphone_Ios_Cocoa Touch - Fatal编程技术网

Iphone 如何在没有振动的情况下在iOS上播放系统声音?

Iphone 如何在没有振动的情况下在iOS上播放系统声音?,iphone,ios,cocoa-touch,Iphone,Ios,Cocoa Touch,在的文档中,它说我可以在播放声音时禁用振动: iPhone播放指定的声音。如果用户已配置 设置应用程序对环的振动,也调用振动。 但是,如果应用程序的音频会话被激活,则设备不会振动 配置了AVAudioSessionCategoryPlayAndRecord或 A音频会话类别记录音频会话类别 然而,即使在将我的分类设置为“播放和录制”之后,我仍然会在iPhone4S(iOS 5.1.1)上感觉到震动。它同时发出响声和振动 #import <AudioToolbox/AudioServices

在的文档中,它说我可以在播放声音时禁用振动:

iPhone播放指定的声音。如果用户已配置 设置应用程序对环的振动,也调用振动。 但是,如果应用程序的音频会话被激活,则设备不会振动 配置了AVAudioSessionCategoryPlayAndRecord或 A音频会话类别记录音频会话类别

然而,即使在将我的分类设置为“播放和录制”之后,我仍然会在iPhone4S(iOS 5.1.1)上感觉到震动。它同时发出响声和振动

#import <AudioToolbox/AudioServices.h>
#import <AVFoundation/AVAudioSession.h>

NSError* error;
[[AVAudioSession sharedInstance]
    setCategory:AVAudioSessionCategoryPlayAndRecord
    error:&error];
if (error == nil) {
    AudioServicesPlayAlertSound(1000);
}
#导入
#进口
n错误*错误;
[[AVAudioSession sharedInstance]
设置类别:AVaudioSessionCategory播放和记录
错误:&错误];
如果(错误==nil){
AudioServicesPlayAlertSound(1000);
}

我也尝试了
AudioServicesPlaySystemSound
,但结果是一样的。我想禁用振动的原因是因为我正在制作一个应用程序,用户必须将手机放在远处,手机不应因振动而翻倒。

您是否尝试过播放自己的声音效果,而不是苹果内置的声音效果

如果您在应用程序中上载音频文件,则可以通过AudioToolBox框架轻松使用该文件 你不必再担心振动了

#import <AudioToolbox/AudioServices.h>    

//Initialise your soundID (your app delegate is a good place for this)    
SystemSoundID soundToPlay;

NSString *path  = [[NSBundle mainBundle] pathForResource:@"<YourFileName>" ofType:@"aif"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &soundToPlay);

//playback
AudioServicesPlaySystemSound(soundToPlay);
#导入
//初始化您的soundID(您的应用程序代理是一个很好的地方)
SystemSoundID soundToPlay;
NSString*path=[[NSBundle mainBundle]pathForResource:@“aif”类型的“@”;
NSURL*pathURL=[NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((uu桥CFURLRef)路径URL和soundToPlay);
//回放
AudioServicesPlaySystemSound(声音播放);

我用我的iPhone 4s测试了这个,它完全符合你的要求

   NSError* error;
    [[AVAudioSession sharedInstance]
     setCategory:AVAudioSessionCategoryPlayAndRecord
     error:&error];
    if (error == nil) {
        SystemSoundID myAlertSound;
        NSURL *url = [NSURL URLWithString:@"/System/Library/Audio/UISounds/new-mail.caf"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &myAlertSound);

        AudioServicesPlaySystemSound(myAlertSound);

    }
使用
AudioServicesCreateSystemSoundID
使用系统声音的文件名创建
SystemSoundID
。然后使用
AudioServicesPlaySystemSound()
播放无振动的声音。其他系统声音的文件名可以在下面的链接中找到


您是否检查了@RDC?您发布的链接中的解决方案也使用了
AudioServicesPlaySystemSound
。它在某些声音ID(如1000)上存在振动的问题(电子邮件接收的声音效果)。我在文档中看到的唯一公开常量是
kSystemSoundID\u振动
。您是如何将“1000”作为合法的东西来传递的
AudioServicesPlaySystemSound()
?(这对我也有效,但这似乎很脆弱。)似乎苹果公司将这种振动硬编码为特殊的声音。由于这些是私人声音,苹果公司似乎不需要它们来遵循您上面引用的文档。@JonBrooks系统声音常数的全部集合都在这里:@JoJo您知道Digistial使用的一些特定声音吗?我过去用我自己的声音做过,没有振动,但这次我想播放内置系统声音。你知道有什么办法能抓住这个机会吗?我知道流行的应用程序Digisocial从某处得到了它们。他们使用了很多无振动的系统声音。我明白了。我还要做一些测试!注意:阅读此问题/答案时,我担心这些似乎没有正式记录/公开支持的系统声音是否会导致应用程序无法通过审查过程。此后,我发现并记录了一些开发人员使用系统声音的案例,他们的应用程序通过了审查流程。举个例子,这很有效。我建议在某个时候调用
AudioServicesDisposeSystemSoundID
,以释放资源。我还将创建我已经播放过的声音的散列图,这样我就不会一遍又一遍地重新创建相同的声音资源。