如何在iPhone应用程序中播放闹钟音乐?

时间:2012-12-11 11:29:26

标签: iphone objective-c ios uilocalnotification

您好我正在使用闹钟应用程序,并设置LocalNotifications警报音乐,如铃声。我完美地播放铃声。但问题是在点击AlertView中的“确定”按钮后,铃声正在播放。如何立即播放以接收LocalNotification

我正在使用以下代码播放铃声:

-(void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification 

{

      application.applicationIconBadgeNumber = 0;

      NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey];

      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Mitwa.mp3", [[NSBundle mainBundle] resourcePath]]];

      NSError *error;
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
      player.numberOfLoops = -1;

      if (player == nil)
        NSLog([error description]);
      else
        [player play];

      [viewController showReminder:@"Good Evening Mahesh!"];

  }

请在收到通知后帮我报警。

...谢谢

3 个答案:

答案 0 :(得分:4)

您不能这样做,您只能在soundName上设置UILocalNotification

如文档中所述:

  

对于此属性,请指定应用程序主包中的声音资源的文件名(包括扩展名)或UILocalNotificationDefaultSoundName以请求默认系统声音。当系统显示本地通知的警报或标记应用程序图标时,它会播放此声音。默认值为nil(无声音)。不支持持续时间超过30秒的声音。如果您指定的声音播放时间超过30秒,则会播放默认声音。

此外,该文件必须位于应用程序包中,因此在构建应用程序时必须存在该文件。

答案 1 :(得分:1)

使用UILocalNotification我们可以设置闹钟,UILocalNotification你可以设置声音,但声音应该只有30秒。你不能播放超过30秒的声音用于本地通知。如果您在点击提醒后想要歌曲,我们可以在您的应用中播放歌曲。

答案 2 :(得分:1)

首先将.mp3个文件或wav包含到您的xcode项目或资源中,然后在声明警报设置的方法中使用UILocalNotification

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.soundName = yourSoundName;
相关问题