【问题标题】:Custom iOS UILocalNotification sound does not play (possibly related to Xcode update)自定义 iOS UILocalNotification 声音不播放(可能与 Xcode 更新有关)
【发布时间】:2012-06-27 22:27:03
【问题描述】:

我正在尝试在UILocalNotification 上获得自定义声音,但我根本没有声音。如果我使用UILocalNotificationDefaultSoundName,我确实得到了默认声音,但是当指定自定义声音时,没有声音,只有消息。据我所知,声音不到 30 秒,而且格式正确。这是文件信息的屏幕截图:

我检查了 XCode 的 DerivedData 目录中的 .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];

【问题讨论】:

    标签: objective-c ios uilocalnotification


    【解决方案1】:

    您的代码似乎不错。

    尝试清理您的项目,从您的设备/模拟器中卸载您的应用,然后重新安装。它可能会有所帮助:)

    【讨论】:

      【解决方案2】:

      不知道是什么原因(也没有看文档),只是打开了动作属性notification setHasAction:YES,声音就开始播放了。

      【讨论】:

        【解决方案3】:

        请确保 iPhone 没有处于静音模式(因为您的代码看起来不错)

        只需检查 iPhone 侧面的按钮

        【讨论】:

        • 正如 UILocalNotificationDefaultSoundName 工作并产生声音这一事实所表明的那样,静音模式不是问题 :)
        【解决方案4】:

        好的,这就是发生的事情。如果它仍在运行,我忘记了应用程序如何处理通知本身。我的代码只显示 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> {
        

        我不确定这是否是最好的方法,因此请随时提出任何改进意见。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 这不是一个完整的解决方案,不应作为答案发布。使用 cmets 提出可能的问题修正建议。
          【解决方案6】:

          你的代码很好, 但请检查您的 iPhone 设置 设置 -> 通知中心 -> 你的应用程序 -> 声音 -> “开启” 声音应该是“开”

          因此,要启用此功能,请在应用程序的 Targets 中的 Capabilities 中选中 Inter App Audio,它在 Inter-app audio 中为 Off Capabilities 将其更改为开启

          然后本地通知声音正在工作 inshallah :)

          【讨论】:

          • 您应该将此作为评论发布。
          猜你喜欢
          • 2013-10-20
          • 2017-10-20
          • 1970-01-01
          • 1970-01-01
          • 2018-10-16
          • 2015-07-09
          • 1970-01-01
          • 1970-01-01
          • 2016-12-23
          相关资源
          最近更新 更多