I'm using UILocalNotification
in an application.
我在应用程序中使用UILocalNotification。
In the application there are two sounds which are played conditionally- I have applied proper conditions for them.
在应用程序中有两种声音有条件地播放 - 我已经为它们应用了适当的条件。
But when I install the application and run it on an iOS 7
device, then it fires the local notification but a sound is not playing in the application.
但是当我安装应用程序并在iOS 7设备上运行它时,它会触发本地通知,但声音不会在应用程序中播放。
Code is given below to set the notification:
下面给出了代码来设置通知:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = [pickerView date];
localNotification.timeZOne= [NSTimeZone defaultTimeZone];
if (alarm_number == 1) {
localNotification.alertBody = [NSString stringWithFormat:@"First Alarm"];
}
else
{
localNotification.alertBody = [NSString stringWithFormat:@"Second Alarm"];
}
localNotification.alertAction =@"Ok";
NSString *message = [[NSString alloc]initWithString:@""];
if(alarm_number == 1)
{
localNotification.soundName=@"Alarm_1.mp3";
message = @"First Alarm Scheduled";
}
else
{
localNotification.soundName=@"Alarm_2.mp3";
message = @"Second Alarm Scheduled";
}
localNotification.applicatiOnIconBadgeNumber= 1;
// Specify custom data for the notification
NSString *alarmString;
if (alarm_number==1) {
alarmString = [NSString stringWithFormat:@"First Alarm"];
}
else
{
alarmString = [NSString stringWithFormat:@"Second Alarm"];
}
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:alarmString forKey:@"AlarmFor"];
localNotification.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
What I have checked for is the Sound setting in the Settings/Notification Centre app for my app.
我检查过的是我的应用程序的设置/通知中心应用程序中的声音设置。
Please go through 1st to 3rd image to see what I have checked.
请查看第1张到第3张图片以查看我查看过的内容。
(1) Notification Center
(1)通知中心
(2) Application
(2)申请
(3) Sound is off here
(3)声音在这里关闭
So, to enable this I have checked Inter App Audio at Capabilities in Targets of the application and it was Off as shown in the image below.
因此,为了启用此功能,我已在应用程序的目标功能中检查了Inter App Audio,它已关闭,如下图所示。
Capabilities in Inter-app audio
应用间音频的功能
Then I have changed it to On and it looks like shown in the image below.
然后我将其更改为On,它看起来如下图所示。
Yet, it still does not play any sound in iOS 7 devices.
然而,它仍然没有在iOS 7设备中播放任何声音。
Does anybody have any idea about why is it not working? It would be a great help.
有没有人知道它为什么不起作用?这将是一个很大的帮助。
Thanks.
谢谢。
9
Did you try to turn off "Do not disturb" mode off? I have the same problem, then I found out "Do not Disturb" is on. After turn off that, it all works the right way.
您是否尝试关闭“请勿打扰”模式?我有同样的问题,然后我发现“请勿打扰”。关闭后,一切正常。
2
try using .caf
file instead of .mp3
尝试使用.caf文件而不是.mp3
afconvert -f caff -d LEI16@44100 -c 1 wolf.wav wolf_out.caf
1
My guess is your sound isn't formatted appropriately to play by the OS. Make sure your sound is formatted in IMA4 format. Also, make sure your file is in your application bundle, and you should be good to go.
我的猜测是你的声音格式不适合操作系统播放。确保您的声音格式为IMA4格式。另外,请确保您的文件位于应用程序包中,您应该很高兴。
For more reference and similar question, see this SO question.
有关更多参考和类似问题,请参阅此SO问题。
Choose custom sound for Local Notifications
为本地通知选择自定义声音
Here is a link for Apple's Local and Push Notification Guide. It explains the sounds in detail and the default sounds that can be used.
以下是Apple的本地和推送通知指南的链接。它详细解释了声音和可以使用的默认声音。
1
I ran into the same issue, I had to do the following steps:
我遇到了同样的问题,我不得不做以下步骤:
Convert the file from mp3 or wav to caf. As stated in other answers you can convert wav, aif and mp3 files in terminal using the command:
将文件从mp3或wav转换为caf。如其他答案中所述,您可以使用以下命令在终端中转换wav,aif和mp3文件:
afconvert -f caff -d LEI16@44100 -c 1 in.mp3 out.caf
afconvert -f caff -d LEI16 @ 44100 -c 1 in.mp3 out.caf
You can also export with Quicktime 7 as .aif with the following settings Linear PCM Rate 44.100 Linear PCM sample size 16 these aif files seem to be supported.
您还可以使用以下设置将Quicktime 7导出为.aif线性PCM速率44.100线性PCM样本大小16似乎支持这些aif文件。
Make sure you files are less than 30 seconds, i.e 29 seconds or less.
确保文件少于30秒,即29秒或更短。
Add you files to the root of your bundle, you can drag drop into Xcode or right click "add files to (projectname)" Note you must select the following options: a)Destination Copy items if needed b)Add to targets (your build name(s)) 4)Reference the file by its name including the extension e.g notif.soundName = @"sound.caf";
将文件添加到捆绑包的根目录,您可以将其拖放到Xcode中或右键单击“将文件添加到(项目名称)”注意您必须选择以下选项:a)目标复制项目(如果需要)b)添加到目标(您的构建) name(s))4)通过名称引用文件,包括扩展名,例如notif.soundName = @“sound.caf”;
Make sure you delete the app from the device / simulator between tests or you will not see the results of your changes.
确保在测试之间从设备/模拟器中删除应用程序,否则您将看不到更改的结果。
If your still having problems, check the sound file file is in the root of the project. Check the Sound file "Target Membership" is selected in the show file inspector.
如果仍有问题,请检查声音文件文件是否在项目的根目录中。检查声音文件在目标文件检查器中选择“目标成员”。
0
I didn't get any idea why it did work.
我不知道它为什么会起作用。
But, at last I have solved my problem by making a new project xcode 5.0 and ported old project to this new project and this did work. So It's working and I can get sound too.
但是,最后我通过创建一个新项目xcode 5.0并将旧项目移植到这个新项目来解决我的问题,这确实有效。所以它工作,我也可以听到声音。
Hopefully it can work for others too.
希望它也适用于其他人。
0
Please try first whether setting->notificationcenter->sounds is not off.Then check whether in setting->sounds->slider is not on zero.That means if u have sounds on zero that will not allow sound to come up. Hope that helps.
请先尝试设置 - > notificationcenter->声音是否关闭。然后检查设置 - >声音 - >滑块是否不为零。这意味着如果你的声音为零,则声音不会出现。希望有所帮助。
0
Check that the "Sound Effects" is not mute or low. The volume may be high but if the Sound Effects volume is mute then the local notification may not sound. U can increase it when you are in the Home screen of iOS and then press the volume up button.
检查“声音效果”是不是静音还是低音。音量可能很高但如果音效音量静音,则可能听不到本地通知。你可以在iOS的主屏幕中增加它,然后按音量增大按钮。
0
If mp3 file is edited with some tool, it is possible that something was missed by edit tool.
如果使用某个工具编辑mp3文件,编辑工具可能会遗漏某些内容。
I had similar problem while trying to play mp3 file as local notification sound. Sound was shorter than 30 seconds, it was bundled, everything appears regular on the surface. After few hours i have realised that problem was caused by online mp3 cutter tool. I have find better editor, and file became playable on notification.
尝试播放mp3文件作为本地通知声音时,我遇到了类似的问题。声音短于30秒,它被捆绑在一起,表面上的一切都显得规则。几个小时后,我意识到问题是由在线mp3切割工具引起的。我找到了更好的编辑器,并且文件在通知时变得可玩了。
0
Make sure the sound is added to the main bundle!
确保声音已添加到主捆绑中!