【问题标题】:iPhone - play alarm sound while the app in backgroundiPhone - 在应用程序在后台播放警报声
【发布时间】:2012-03-11 11:34:48
【问题描述】:

你知道如何在 iPhone 睡眠时播放闹钟声音吗? 喜欢 iPhone 中的内置时钟应用程序吗?

非常重要的编辑:
在 iPhone 的内置时钟应用程序中
播放警报声时,如果用户将静音开关切换到静音(振动模式),
警报声仍在继续播放。

你知道怎么做吗?

【问题讨论】:

  • 我已经用了几个月了,看看my thread。

标签: iphone objective-c background alarm


【解决方案1】:

唯一的方法是使用local Notifications 或Push notifications..都允许声音播放30秒

【讨论】:

  • 内置应用程序(本机 iOS 应用程序)没有 3rd 方 iOS 应用程序的限制......所以简而言之......如果用户切换到静音,您的应用程序声音将不会播放。 .
  • 但是这个免费的应用程序可以在后台播放声音,当用户切换到静音时不会静音:itunes.apple.com/us/app/i-qi-singing-bowl-timer-free/…,你知道他们是怎么做到的吗?
  • 不知道..虽然我会建议做一个示例项目并使用本地通知来检查自己..只需要 15 分钟..
  • 感谢 Shubhank 一直以来的支持。实际上我只是这样做了,当我将开关切换到静音模式后,我的 UILocalNotification 声音(26 秒)立即停止(播放时)。 i-iQ 应用程序必须采取其他措施来阻止声音停止播放。如果你能告诉我怎么做,我将不胜感激。谢谢。
  • 不认识的人..我知道这是不可能的..如果按一下静音..即使铃声不播放..i-IQ 应用程序将如何玩??(疯狂的东西!!)
【解决方案2】:

设置按钮被连接起来以在视图控制器中运行一个名为scheduleNotification 的方法,该方法使用UILocalNotification 类来安排通知。代码如下:

(void)scheduleNotification
{
    [reminderText resignFirstResponder];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = [datePicker date];
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.alertBody = @"Did you forget something?";
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
              forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }
}

【讨论】:

  • @eeerahul:感谢编辑我的答案和@Tuyen Nguyen:对不起兄弟,我不知道这个,但我认为它不能在静音模式下播放声音兄弟。如果你真的想感谢我,请点击向上箭头按钮 thnx
  • @TuyenNguyen:还有一件事,如果你可以改变声音模式,那么你可以做任何你想做的事......我想我永远不会发生哈哈。祝你好运
【解决方案3】:

在应用程序进入后台之前,使用 AVAudioPlayer 播放无声的声音并使其无限次播放。当您的通知启动时,再次使用 AVAudioPlayer 播放您的通知声音,而不是 UILocalNotification 对象。它对我有用。

【讨论】:

    【解决方案4】:

    所以看看 i-qi 应用,很可能他们在 info.plist 中将 UIBackgroundModes 设置为“音频”。这意味着他们能够在后台播放音频。他们可能一直在播放无声音频,直到计时器结束,因为如果不使用后台音频会话将被切断。

    就静音切换而言,这可能基于他们使用的会话类别。看看这个资源: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

    【讨论】:

      【解决方案5】:

      此代码将帮助您在后台播放音乐: 音乐的声音文件应该只有 30 秒 通知支持 30 秒的声音文件,如果它在文件夹中,它应该在 bundle 文件夹中,那么你必须放置声音文件的路径

      NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
      NSString *dateValueRemaining =[NSString stringWithFormat:@"23/08/2012 11:30:33"];
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
      NSDate *dateRemaining = [dateFormat dateFromString:dateValueRemaining];
      NSDate *pickerDate = dateRemaining;
      NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit
                                                               | NSMonthCalendarUnit
                                                               | NSDayCalendarUnit)
                                                     fromDate:pickerDate];
      NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit
                                                               | NSMinuteCalendarUnit
                                                               | NSSecondCalendarUnit)
                                                     fromDate:pickerDate];
      // Set up the fire time
      NSDateComponents *dateComps = [[NSDateComponents alloc] init];
      [dateComps setDay:[dateComponents day]];
      [dateComps setMonth:[dateComponents month]];
      [dateComps setYear:[dateComponents year]];
      [dateComps setHour:[timeComponents hour]];
      [dateComps setMinute:[timeComponents minute]];
      [dateComps setSecond:[timeComponents second]];
      NSDate *itemDate = [calendar dateFromComponents:dateComps];
      NSLog(@"itemDate: %@", itemDate);
      if (localNotif) {
          [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
      }
      localNotif = [[UILocalNotification alloc] init];
      if (localNotif == nil) {
          return;
      }
      UILocalNotification *localNotif = [[UILocalNotification alloc] init];
      localNotif.fireDate = itemDate;
      localNotif.timeZone = [NSTimeZone defaultTimeZone];
      localNotif.alertBody = @"Your Time is Over";
      localNotif.alertAction = @"View";
      //---THIS SONG FILE IN THE NSBUNDLE FOLDER---------//
      localNotif.soundName = @"s1.mp3";
      NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
      localNotif.userInfo = infoDict;
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 2011-04-16
        相关资源
        最近更新 更多