我正在为iPhone编写一个提醒应用程序,它使用本地通知显示提醒。
如果在应用程序运行时发出提醒,则不会显示本地通知。相反,在我的app委托中调用didReceiveLocalNotification方法,我通过显示带有提醒文本的UIAlertView来模拟本地通知对话框。
当本地通知显示在应用程序之外时,设备就会震动,UILocalNotificationDefaultSoundName指定的声音也会被播放。同样,在显示UIAlertView时,我也想在应用程序中模仿这一点。
我可以通过调用AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)来震动设备,但是我想不出如何播放本地通知的默认声音。没有等效的SystemSoundID常数,我也不确定路径是什么。
tl;博士我想在显示UIAlertView时播放本地通知默认声音。有什么想法吗?
发布于 2010-08-19 19:09:44
问得好。理想情况下,可以使用AudioServices选择系统声音。然而,苹果的“系统声音服务参考”中的以下声明表明情况并非如此:
在Mac中,当用户将系统首选项配置为闪存警报屏幕时,或者如果声音无法呈现,调用此函数将导致屏幕闪烁。在Mac中,通过常量kSystemSoundID_UserPreferredAlert播放用户在系统首选项中选择的警报声音。In iOS没有首选的用户警报声音.
因为SDK似乎没有什么可提供的,所以您可能希望通过使用自己的wav文件来模拟系统的声音。在下面的链接中有一个很好的库,它可能有您想要的声音:WAV.zip
祝好运!
发布于 2015-07-10 17:01:06
您可以这样播放默认通知声音:
AudioServicesPlaySystemSound(1315);这里,您将找到可以用作AudioServicesPlaySystemSound(id)参数的id列表。
很好的编码!
发布于 2012-10-24 06:35:14
在.h文件中设置委托:
@interface ViewController : UIViewController <UIAlertViewDelegate>
{
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end以及上面声明的set方法。
在.m文件中执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ma.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
[audioPlayer stop];
}
NSLog(@"U HAVE CLICKED BUTTON");
}https://stackoverflow.com/questions/3277811
复制相似问题