【发布时间】:2011-10-08 20:59:41
【问题描述】:
首先,我对目标 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 xcode audio vibration