音频和振动在模拟器中工作,而不是在 ios 设备上工作
首先,我对 Objective C 还很陌生。我想在特定条件下播放声音并振动。我的 iPhone 4 没有静音,我检查了大小写敏感性,并使用 AVAudioPlay 和 SystemSound 尝试播放文件,但即使振动也不起作用。 AudioToobox 和 AVfoundation 在 .h 中导入。音频在 iOS 模拟器上使用两种方法播放。
代码
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);}
以下是使用 SystemSound:和使用 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 文件?任何帮助将不胜感激。
First off, I'm fairly new with objective C. I want to play a sound and vibrate under a certain condition. My iPhone 4 is not on mute, I've checked case sensitivity and used both AVAudioPlay and SystemSound to try and play the file but even the vibrate will not work. AudioToobox and AVfoundation are imported in .h. The audio plays with both methods on the iOS simulator.
Heres the code using 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);}
And with 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];}
By the way, the variable al sets to 0 after it plays. Is my coding wrong to have the sound and vibrate work on the device itself? Is there a specific type of .wav file that needs to be played? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题。实际上有一个专门用于警报的第二个音量设置,至少在我的情况下被设置为静音,即使我的常规音量已调高。查看设备上“设置”应用程序中的声音菜单并将其打开。
尽管声音的实际播放很重要,但您可能不想对实际发布的应用程序使用警报,因为它将要求用户确保此单独的音频设置已启动。
I was running into this same issue. There is actually a second volume setting specially for alerts that at least in my case was set to mute, even though my regular volume was up. Take a look under the sounds menu in the Settings app on the device and turn that up.
You probably don't want to use the alerts for an actual released app though if it is important that the sound actually play, since it will require users to make sure this separate audio setting is up.