音频和振动在模拟器中工作,而不是在 ios 设备上工作

发布于 2024-12-08 14:24:33 字数 1140 浏览 0 评论 0原文

首先,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-12-15 14:24:33

我遇到了同样的问题。实际上有一个专门用于警报的第二个音量设置,至少在我的情况下被设置为静音,即使我的常规音量已调高。查看设备上“设置”应用程序中的声音菜单并将其打开。

尽管声音的实际播放很重要,但您可能不想对实际发布的应用程序使用警报,因为它将要求用户确保此单独的音频设置已启动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文