显示 UIAlertView 时播放本地通知默认声音?
我正在为 iPhone 编写一个提醒应用程序,它使用本地通知显示提醒。
如果在应用程序运行时发出提醒,则不会显示本地通知。相反,在我的应用程序委托中调用 didReceiveLocalNotification
方法,并通过显示带有提醒文本的 UIAlertView
来模拟本地通知对话框。
当本地通知显示在应用程序外部时,设备会振动并发出 UILocalNotificationDefaultSoundName
已播放。同样,我想在显示 UIAlertView
时在应用程序中模仿这一点。
我可以通过调用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
来振动设备,但我不知道如何播放本地通知默认声音。没有等效的 SystemSoundID 常量,并且我不确定路径是什么。
tl;dr 我想在显示 UIAlertView 时播放本地通知默认声音。有什么想法吗?
I'm writing a reminders app for iPhone that displays reminders using local notifications.
If a reminder goes off while the application is running, the local notification isn't displayed. Instead, the didReceiveLocalNotification
method is called in my app delegate, and I mimic the local notification dialog by displaying a UIAlertView
with the reminder text.
When local notifications are displayed outside of the app, the device vibrates and the sound specified by UILocalNotificationDefaultSoundName
is played. Again, I'd like to mimic this in the app when displaying the UIAlertView
.
I can vibrate the device by calling AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
, but I can't figure out how to play the local notification default sound. There's no equivalent SystemSoundID constant, and I'm not sure what the path would be.
tl;dr I'd like to play the local notification default sound when displaying a UIAlertView. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好问题。理想情况下,有一种使用音频服务选择系统声音的方法。然而,Apple 的“系统声音服务参考”中的以下声明却表明了相反的情况:
由于 SDK 似乎没有什么可提供的,您可能希望使用自己的 wav 文件来模仿系统声音。以下链接中有一个不错的库,也许它会提供您正在寻找的声音:http://sites.google.com/site/iphonesounds/iPhoneOriginalSystemSounds_WAV.zip
祝你好运!
Good question. Ideally, there would be a way of selecting a system sound using AudioServices. However, the following statement from Apple's "System Sound Services Reference" suggests otherwise:
Since it seems like the SDK has little to offer, you might wish to mimick the system sounds by using your own wav file. There is a nice library at the following link, perhaps it will have the sound you're looking for: http://sites.google.com/site/iphonesounds/iPhoneOriginalSystemSounds_WAV.zip
Good luck!
您可以通过以下方式播放默认通知声音:
在这里您将找到您可以将 ids 用作 AudioServicesPlaySystemSound(id) 的参数。
良好的编码!
You can play the default notification sound in this way:
Here you'll find the list of the ids you can use as parameter of AudioServicesPlaySystemSound(id).
Good coding!
在.h文件中设置委托:
以及上面声明的设置方法。
在 .m 文件中执行以下操作:
Set delegate in .h file:
And set method that above declared.
And in .m file do this:
这是对安德鲁·利特尔答案的补充。
为了更好地模仿通知声音,您还应该配置音频会话:
这在您听音乐并在此期间获取通知时至关重要。
上面的会话参数看起来与后台应用程序触发通知时播放的声音相同:
This is addition to Andrew Little answer.
To better mimic notification sound, you also should to configure audio session:
This is essential when e.g. you listen to the music and obtain notification during this.
Parameters of session above looks the same as for sound played when notification fires with application in background: