V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
xatest
V2EX  ›  iDev

iOS的本地通知自定义声音设置无效~

  •  
  •   xatest ·
    aoxu · 2012-05-09 01:14:14 +08:00 · 8734 次点击
    这是一个创建于 4371 天前的主题,其中的信息可能已经有所发展或是发生改变。
    相关代码:
    UILocalNotification *newNotification = [[UILocalNotification alloc] init];
    if (newNotification) {
    newNotification.fireDate = newFireDate;
    newNotification.alertBody = @"查看闹钟:你妈妈喊你回家吃饭!";
    newNotification.soundName = @"wakeup.caf";
    newNotification.alertAction = @"alarm now!!!";
    newNotification.repeatInterval = NSWeekCalendarUnit;
    newNotification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
    NSLog(@"Post new localNotification:%@", [newNotification fireDate]);
    }

    上面代码中的@"wakeup.caf"是资源文件夹中的一个不到10秒的声音文件,单独点击试过可以播放。但是当通知响起时,播放的依然是系统默认的三全音,而没有播放设置的wakeup.caf,请问可能是什么原因?
    21 条回复    1970-01-01 08:00:00 +08:00
    xatest
        1
    xatest  
    OP
       2012-05-09 01:48:29 +08:00
    在设置声音前一句加了一句调试日志:
    NSString* soundPath = [[NSBundle mainBundle] pathForResource:@"wakeup" ofType:@"caf"];
    NSLog(@"soundPath:%@", soundPath);
    输出结果为soundPath:(null)
    为什么?
    Mattsive
        2
    Mattsive  
       2012-05-09 01:52:20 +08:00
    你确认加入 project 了么?Build Phases/Copy Bundle Resources 里看看有没有这个文件,还有就是注意大小写
    xatest
        3
    xatest  
    OP
       2012-05-09 02:02:55 +08:00
    @Mattsive 确认加入project了,我现在只开了这一个project,播放声音我都是在Xcode的这个project内点的~
    文件是全小写,注意区分了的~
    Build Phases/Copy Bundle Resources在哪里能看到?
    Mattsive
        4
    Mattsive  
       2012-05-09 02:05:46 +08:00
    点 Project 名,右边 TARGETS/{Project Name}

    有时候 Xcode 能看到资源文件也不一定就加入 Bundle Resources 了,这个问题我遇到过
    xatest
        5
    xatest  
    OP
       2012-05-09 02:14:54 +08:00
    @Mattsive 在Build Phases/Copy Bundle Resources确实没有这个声音,我加进去了,然后新的问题是NSLog(@"soundPath:%@", soundPath);这一句日志打不出来了,前后的其他调试日志都能打出来。
    Mattsive
        6
    Mattsive  
       2012-05-09 02:18:56 +08:00
    你把 soundPath 换成 dummy string 呢,检查有没有执行到打印这一句?设个断点单步跟踪下吧
    xatest
        7
    xatest  
    OP
       2012-05-09 02:26:03 +08:00
    @Mattsive 试了一下不管是不是dummy string,我单步跟踪时,这句执行了,我故意step in想看看NSLog进去怎么执行的,结果直接等于Step over跳到下一句了,日志还是没出来~
    Mattsive
        8
    Mattsive  
       2012-05-09 02:34:14 +08:00
    奇葩啊。。介意把这段代码贴出来么
    xatest
        9
    xatest  
    OP
       2012-05-09 02:47:11 +08:00
    @Mattsive 代码现在写成:
    UILocalNotification *newNotification = [[UILocalNotification alloc] init];
    if (newNotification) {
    newNotification.fireDate = newFireDate;
    newNotification.alertBody = @"查看闹钟:你妈妈喊你回家吃饭!";
    NSString* soundPath = [[NSBundle mainBundle] pathForResource:@"wakeup" ofType:@"caf"];
    NSLog(@"soundPath:%@", soundPath);
    newNotification.soundName = soundPath;
    newNotification.alertAction = @"alarm now!!!";
    newNotification.repeatInterval = NSWeekCalendarUnit;
    [[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
    NSLog(@"Post new localNotification:%@", [newNotification fireDate]);
    }
    神奇的是我把打不出来的NSLog那句删掉重写了一遍,日志就能打出来了,结果是
    2012-05-09 02:45:39.570 CuteAlarm[2106:707] soundPath:/var/mobile/Applications/DBA87511-821F-4D0E-AB22-78FD843BB122/CuteAlarm.app/wakeup.caf
    但是通知出现时,依然没有播放这段声音~
    Mattsive
        10
    Mattsive  
       2012-05-09 02:54:32 +08:00
    好吧,原来是在真机上测试啊,代码阶段还是用模拟器比较好。

    不能播放声音可能是格式不对,Xcode 能播放不代表符合要求,Notification 的声音支持格式很少的,只有 Linear PCM,MA4 (IMA/ADPCM),µLaw,aLaw 四种。检查下是不是这个问题。
    Mattsive
        11
    Mattsive  
       2012-05-09 02:57:02 +08:00
    真机测试 ProTip No.1,有时候遇到很奇葩的问题,Product/Clean 一下可能会有奇效。
    xatest
        12
    xatest  
    OP
       2012-05-09 03:01:05 +08:00
    @Mattsive 嗯是真机调试,原因是这个APP有些特性必须要真机测试,比如重力感应、震动等等。我用模拟器试了一下,通知出来时没有任何声音。
    这个声音文件的格式应该是没问题的,我是从另一个APP copy过来做测试用的,那个APP使用同样的声音,不论真机还是模拟器,都可以在通知中播放(为了减少错误的可能性,我连代码都是尽量照搬的)。
    xatest
        13
    xatest  
    OP
       2012-05-09 03:06:12 +08:00
    @Mattsive 这个我也试过,顺便多说一招:点菜单的Product,再按住Option键,这时候Clean会变成Clean Build Folder...
    请教一下,这种格式很少见的Notification声音一般是在哪找的?或者怎么生成的?
    Mattsive
        14
    Mattsive  
       2012-05-09 03:06:13 +08:00
    好吧。。再检查下通知中心的设置,app 对应的“声音”选项是不是 On
    Mattsive
        15
    Mattsive  
       2012-05-09 03:07:31 +08:00
    afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v

    Local and Push Notification Programming Guide 文档有介绍
    xatest
        16
    xatest  
    OP
       2012-05-09 03:07:53 +08:00
    @Mattsive 模拟器的设置里没有通知中心。。。模拟器版本5.1 (9B176)
    xatest
        17
    xatest  
    OP
       2012-05-09 03:10:49 +08:00
    @Mattsive 这篇Guide只草草看了Local Notification的部分,因为暂时用不到Push Notification就没仔细看全篇。今天先到这里,明天继续debug。非常感谢!
    Mattsive
        18
    Mattsive  
       2012-05-09 03:14:17 +08:00
    caf 发我 ceator at gmail,我试试
    xatest
        19
    xatest  
    OP
       2012-05-09 10:07:12 +08:00
    @Mattsive 得今晚发给你,我的gtalk是xatest at gmail
    xatest
        20
    xatest  
    OP
       2012-05-11 01:20:42 +08:00
    @Mattsive 昨天电脑出了点问题,发不了声音文件,今天我找到问题原因,是由于这个声音文件是我之前从另一个Project的左侧文件导航树里直接拖动到现在这个Project里的,我还右键-show in Finder看过这个文件确实在新Project的文件夹里。但是这样会导致找不到这个资源。
    正确的做法是,删除这个文件,然后使用菜单File-Add Files to "xxxproject",从别的目录选择一个文件添加进来,才可以找到资源。这个办法虽然比较麻烦,但是是确认有效的。
    Mattsive
        21
    Mattsive  
       2012-05-11 02:01:56 +08:00
    果然还是和 bundle 有关,问题解决就好。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2741 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:17 · PVG 18:17 · LAX 03:17 · JFK 06:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.