user1482450
user1482450

Reputation:

how to play alarm music in iphone app?

Hi I'm working with alarm application, and setting LocalNotifications with alarm music something like ringtone. I playing ringtone perfectly. But the problem is after clicking the "Ok" button in AlertView, ringtone is playing. How can I play immediate to receive LocalNotification.

I am using following code to play ringtone :

-(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!"];

  }

Please, help me to play alarm after receiving Notification.

Thanks...

Upvotes: 1

Views: 2004

Answers (3)

ganesh manoj
ganesh manoj

Reputation: 977

Using UILocalNotification we can set alarm and for that UILocalNotification you can set sound but that sound should be only 30 Seconds.You cant play sound which is more than 30 Seconds for local notifications. If you want song after click on alert we can play song in your app.

Upvotes: 1

Augustine93
Augustine93

Reputation: 11

First include the .mp3 files or wav into your xcode project or resources then in the method where you declare alarm settings, use UILocalNotification.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.soundName = yourSoundName;

Upvotes: 0

rckoenes
rckoenes

Reputation: 69469

You can't do that you can only set the soundName on UILocalNotification.

As state in the documentation:

For this property, specify the filename (including extension) of a sound resource in the application’s main bundle or UILocalNotificationDefaultSoundName to request the default system sound. When the system displays an alert for a local notification or badges an application icon, it plays this sound. The default value is nil (no sound). Sounds that last longer than 30 seconds are not supported. If you specify a file with a sound that plays over 30 seconds, the default sound is played instead.

Also the file has to be in the App bundle, thus it must be there when you build your app.

Upvotes: 3

Related Questions