Setting sounds for local notifications on a 3G iPhone running iOS4

 ̄綄美尐妖づ 提交于 2020-01-21 07:40:20

问题


I have successfully scheduled local notifications in my app using the code below:

Class cls = NSClassFromString(@"UILocalNotification");

    if (cls != nil) {

        UILocalNotification *notification = [[cls alloc] init];
        notification.fireDate = self.alarmNotificationDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];

        notification.alertBody = @"Alarm is due";
        notification.alertAction = @"Show Alarm";

        notification.soundName = @"alarm.mp3";

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [notification release];

Where self.alarmNotificationDate is a NSDate assign in another method.

All works fine in the simulator, but when I test on my trusted old iPhone 3G running iOS4 I get the notification, but only with the default sound.

I have tried with different sound files but no success.

Any clue why it could be like this and how to correct it?

Thanks


回答1:


UILocalNotification does not support mp3. It only supports the formats and containers mentioned on the reference page.

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html

Because custom alert sounds are played by the iOS system-sound facility, they must be in one of the following audio data formats:

  Linear PCM
  MA4 (IMA/ADPCM)
  µLaw
  aLaw

You can package the audio data in an aiff, wav, or caf file.




回答2:


There could be a couple of reasons:

  1. Is your sound file included in your bundle? (most likely, seeing as it works on the simulator)
  2. The Apple documentation says to not use compressed audio formats. See the link here. The reason for this being that the iPhone hardware can only play one compressed audio file at a time, and so it is not recommended to use one for the alarm as well, in case the user is already listening to a song from iTunes.



回答3:


Did you spell the name of the sound using the same upper/lower case encoding in both the file name and the filename?



来源:https://stackoverflow.com/questions/3716966/setting-sounds-for-local-notifications-on-a-3g-iphone-running-ios4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!