自定义iOS UILocalNotification声音不播放(可能与Xcode更新有关)

我试图得到一个自定义的声音在UILocalNotification工作,我只是没有听起来。 如果我使用UILocalNotificationDefaultSoundName ,我确实得到默认声音,但是当指定自定义声音时,没有声音,只是消息。 据我所知,声音小于30秒,格式正确。 以下是文件信息的屏幕截图:

我已经检查了XcodeDerivedData目录中的.app目录,而alarm.caf文件位于应用程序的根目录下,我相信这意味着它在包中(对吧?)。

我很确定这是前一阵子,我已经升级了Xcode。 也许这是一个提示?

我也尝试删除/重新安装/重新启动在其他答案中提到的。 正如你所看到的,我首先调用cancelAllLocalNotifications

有没有人有任何想法可能是错的?

 [[UIApplication sharedApplication] cancelAllLocalNotifications]; NSLog(@"installing alarm"); [arguments pop]; // name [arguments pop]; // title alarm.alertBody = [arguments pop]; alarm.fireDate = [[NSDate date] addTimeInterval:[[arguments pop] intValue]/1000]; //alarm.soundName = UILocalNotificationDefaultSoundName; alarm.soundName = @"alarm.caf"; [[UIApplication sharedApplication] scheduleLocalNotification:alarm]; 

你的代码似乎很好。

尝试清理您的项目,从您的设备/模拟器卸载您的应用程序,然后重新安装它。 它可以帮助也许:)

我不知道原因(我也没有阅读文档),我刚刚打开动作属性notification setHasAction:YES ,声音开始播放。

请确保iPhone不处于静音模式(因为您的代码似乎很好)

只需检查iPhone侧面的button

好的,这是发生了什么事。 我忘记了应用程序如何处理通知本身,如果它仍然在运行。 我的代码只显示一个UIAlertView而不是播放声音。 我不知道为什么它使用默认声音。 无论如何,我将这样的代码添加到我的AppDelegate中:

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"didReceiveLocalNotification"); if (application.applicationState == UIApplicationStateActive) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MarkMyTime" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:notification.soundName ofType:nil]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil]; [fileURL release]; player.delegate = self; [player prepareToPlay]; [player play]; [alertView show]; if (alertView) { [alertView release]; } } } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { NSLog(@"Releasing player"); [player release]; } 

这将显示一个UIAlertView并在通知对象上播放声音。 您还需要将AVAudioPlayerDelegate接口添加到AppDelegate以便能够将该委托分配给播放器。 我想如果你使用ARC,这个代码可以简化一下。

 @interface AppDelegate : PhoneGapDelegate <AVAudioPlayerDelegate> { 

我不确定这是否是最好的方法,所以随时提出改进意见。

也许你不在Xcode项目中添加声音文件(* .caf):Build Phases / Copy Bundle Resources。

你的代码是好的,但检查你的iphone设置设置 – >通知中心 – >你的应用程序 – >声音 – >“开”声音应该是“开”

因此,要启用此function,请在应用程序的目标function中检查Inter App Audio,并在应用程序内audioclosuresfunction中将其更改为On

然后当地通知声音工作inshallah 🙂