首先,我对 Objective-C 相当陌生。我想在特定条件下播放声音和振动。我的 iPhone 4 没有静音,我检查了区分大小写并同时使用 AVAudioPlay 和 SystemSound 来尝试播放文件,但即使振动也不起作用。 AudioToobox 和 AVfoundation 在 .h 中导入。在 iOS 模拟器上使用这两种方法播放音频。
这是使用 SystemSound 的代码:
if (al == 1){
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Alarm", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);}
和 AVAudioPlayer:
if (al == 1) {NSString *path = [[NSBundle mainBundle] pathForResource:@"alarmtwo" ofType:@"wav"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.numberOfLoops = 0;
theAudio.volume = 1.5;
[theAudio prepareToPlay];
[theAudio play];}
顺便说一下,变量 al 在播放后设置为 0。让声音和振动在设备本身上工作是我的编码错误吗?是否需要播放特定类型的 .wav 文件?任何帮助将不胜感激。
最佳答案
我遇到了同样的问题。实际上有第二个音量设置专门用于警报,至少在我的情况下设置为静音,即使我的常规音量已调高。在设备上的“设置”应用中查看“声音”菜单下的内容并将其打开。
尽管实际播放声音很重要,但您可能不希望对实际发布的应用使用警报,因为这将要求用户确保此单独的音频设置已启动。
关于objective-c - 音频和振动在模拟器而不是 ios 设备上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7699747/