【问题标题】:Set local notification sound from Main bundle从主包设置本地通知声音
【发布时间】:2012-09-06 05:12:52
【问题描述】:

我为本地通知提供了自定义声音,但它不起作用。

notif.soundName = @"sound.caf";

为什么会播放默认声音? 谢谢。

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我已经使用下面的代码成功完成了通知。

      UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                  if (localNotif == nil)
                      return;
                  /*  NSDateFormatter *formatDate=[[NSDateFormatter alloc]init];
                   [formatDate setDateFormat:@"MM/dd/yyyy"];
                   [formatDate setTimeZone:[NSTimeZone systemTimeZone]];
                   NSDate *strDate=[formatDate dateFromString:txtDate.text];
                   */
                  localNotif.fireDate = itemDate;
                  //localNotif.timeZone = [NSTimeZone systemTimeZone];
      
                  NSLog(@"the item date is %@",localNotif.fireDate);
      
                  // Notification details
                  //localNotif.alertBody = [eventText text];]
                  localNotif.alertBody=strEvent;
                  // Set the action button
                  localNotif.alertAction = @"View";
      
      
                  //localNotif.soundName = @"Acoustic Noodling 01.caf";
                  localNotif.soundName=@"alarm1.aif";
      

      【讨论】:

        【解决方案3】:

        关于在本地通知中播放自定义声音

        确保声音确实在您的应用程序包中,格式正确(线性 PCM 或 IMA4

        假设通知是 UILocalNotification 类型的对象。

        notification.soundName = @"sound.caf";

        如果这最初不起作用,那么您可以参考这篇文章并参考This Answer 这告诉我们使用正确格式的声音文件(即线性 PCM 或 IMA4)。

        【讨论】:

          【解决方案4】:

          只需使用文件的相对路径,而不是提供绝对或完整路径。

          例如:

          localNotification.soundName = @"../Documents/recordedFileName.caf";
          

          另外,请记住文件的 recordDuration 应小于或等于 29 秒。

          就是这样,希望得到答复。

          【讨论】:

          • 这对你有用吗?我试过了,但没有运气
          猜你喜欢
          • 2011-07-05
          • 1970-01-01
          • 1970-01-01
          • 2012-02-06
          • 1970-01-01
          • 2022-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多