简体   繁体   English

从主捆绑设置本地通知声音

[英]Set local notification sound from Main bundle

I give a custom sound to local notification, but it is not working. 我给本地通知一个自定义声音,但它不起作用。

notif.soundName = @"sound.caf";

it's play default sound why? 它播放默认声音为什么? Thanks. 谢谢。

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

I have done notification successfully with the code below. 我已成功通知以下代码。

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";

About playing custom sounds in Local notifications 关于在本地通知中播放自定义声音

Make sure the sound is actually in your app's bundle, is in the correct format (linear PCM or IMA4 确保声音实际上在您的应用程序包中,格式正确(线性PCM或IMA4

Assume that notification is an object of type UILocalNotification. 假设通知是UILocalNotification类型的对象。

notification.soundName = @"sound.caf"; notification.soundName = @“sound.caf”;

If this doesn't work initially then you may refer to this post and there in refer to This Answer That tells us to use sound file of the correct format (ie either Linear PCM or IMA4). 如果这最初不起作用,那么你可以参考这篇文章,参考这个答案告诉我们使用正确格式的声音文件(即线性PCM或IMA4)。

Just use relative path of your file rather than providing absolute or full path. 只需使用文件的相对路径,而不是提供绝对路径或完整路径。

For example: 例如:

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

Also, keep in mind that recordDuration of file should be less than or equal to 29 seconds. 另外,请记住,文件的recordDuration应小于或等于29秒。

That's it, hope that answered. 就是这样,希望得到回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM