热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UILocalNotification自定义声音不在iOS7中播放-UILocalNotificationcustomsoundisnotplayinginiOS7

ImusingUILocalNotificationinanapplication.我在应用程序中使用UILocalNotification。Intheapplicationt

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)通知中心

(1) Notification Center

(2) Application

(2)申请

enter image description here

(3) Sound is off here

(3)声音在这里关闭

enter image description here

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

应用间音频的功能

Capabilities in Inter-app audio

Then I have changed it to On and it looks like shown in the image below.

然后我将其更改为On,它看起来如下图所示。

Capabilities after changed 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 个解决方案

#1


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


2  

try using .caf file instead of .mp3

尝试使用.caf文件而不是.mp3

afconvert -f caff -d LEI16@44100 -c 1 wolf.wav wolf_out.caf

#3


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的本地和推送通知指南的链接。它详细解释了声音和可以使用的默认声音。

#4


1  

I ran into the same issue, I had to do the following steps:

我遇到了同样的问题,我不得不做以下步骤:

  1. 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

  2. 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文件。

  3. Make sure you files are less than 30 seconds, i.e 29 seconds or less.

    确保文件少于30秒,即29秒或更短。

  4. 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.

如果仍有问题,请检查声音文件文件是否在项目的根目录中。检查声音文件在目标文件检查器中选择“目标成员”。

#5


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.

希望它也适用于其他人。

#6


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->声音是否关闭。然后检查设置 - >声音 - >滑块是否不为零。这意味着如果你的声音为零,则声音不会出现。希望有所帮助。

#7


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的主屏幕中增加它,然后按音量增大按钮。

#8


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切割工具引起的。我找到了更好的编辑器,并且文件在通知时变得可玩了。

#9


0  

Make sure the sound is added to the main bundle! enter image description here

确保声音已添加到主捆绑中!


推荐阅读
  • Apple iPad:过渡设备还是平板电脑?
    I’vebeenagonizingoverwhethertopostaniPadarticle.Applecertainlydon’tneedmorepublicityandthe ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • PriorityQueue源码分析
     publicbooleanhasNext(){returncursor<size||(forgetMeNot!null&am ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • Mono为何能跨平台
    概念JIT编译(JITcompilation),运行时需要代码时,将Microsoft中间语言(MSIL)转换为机器码的编译。CLR(CommonLa ... [详细]
  • JVM:33 如何查看JVM的Full GC日志
    1.示例代码packagecom.webcode;publicclassDemo4{publicstaticvoidmain(String[]args){byte[]arr ... [详细]
  • 生产环境下JVM调优参数的设置实例
     正文前先来一波福利推荐: 福利一:百万年薪架构师视频,该视频可以学到很多东西,是本人花钱买的VIP课程,学习消化了一年,为了支持一下女朋友公众号也方便大家学习,共享给大家。福利二 ... [详细]
  • Myappcrashedandthecodeisthefollowing:我的应用程序崩溃,代码如下:elseif(){CGDetailVie ... [详细]
author-avatar
拂袖方言_633
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有