【发布时间】:2012-09-06 05:12:52
【问题描述】:
我为本地通知提供了自定义声音,但它不起作用。
notif.soundName = @"sound.caf";
为什么会播放默认声音? 谢谢。
【问题讨论】:
标签: iphone objective-c
我为本地通知提供了自定义声音,但它不起作用。
notif.soundName = @"sound.caf";
为什么会播放默认声音? 谢谢。
【问题讨论】:
标签: iphone objective-c
也许你没有在 Xcode 项目中添加声音文件 (*.caf):Build Phases/Copy Bundle Resources。
【讨论】:
我已经使用下面的代码成功完成了通知。
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";
【讨论】:
关于在本地通知中播放自定义声音
确保声音确实在您的应用程序包中,格式正确(线性 PCM 或 IMA4
假设通知是 UILocalNotification 类型的对象。
notification.soundName = @"sound.caf";
如果这最初不起作用,那么您可以参考这篇文章并参考This Answer 这告诉我们使用正确格式的声音文件(即线性 PCM 或 IMA4)。
【讨论】:
只需使用文件的相对路径,而不是提供绝对或完整路径。
例如:
localNotification.soundName = @"../Documents/recordedFileName.caf";
另外,请记住文件的 recordDuration 应小于或等于 29 秒。
就是这样,希望得到答复。
【讨论】: