[英]AudioServicesPlayAlertSound not playing custom sound during application:didReceiveRemoteNotification:
我希望其他人对此问题有深刻的了解...
我正在尝试播放自定义声音,该声音已加载到我的应用程序的资源中。 我使用afconvert工具创建了声音,如将音频转换为CAF格式以使用OpenAL线程在iPhone上播放中所建议的那样。 执行以生成我的自定义声音文件的特定命令是:
afconvert ~/Desktop/moof.au ~/Desktop/moof.caf -d ima4 -f caff -v
当我在测试设备(iPhone 5,iOS 7.0或iPhone 4,iOS 6.1.3)上运行我的应用程序并发送包含我的自定义声音文件名称的推送通知时,设备会振动但没有声音播放。
这是有点奇怪的地方...如果我在播放声音的方法的开始处设置一个断点,然后单步执行代码,则设备将播放自定义声音并响应推送通知而振动!
这是有问题的代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// If the app is running and receives a remote notification, the app calls this method to process
// the notification payload. Your implementation of this method should use the app state to take an
// appropriate course of action.
ALog(@"push data package: %@", userInfo);
NSDictionary *payload = [userInfo objectForKey:@"aps"];
NSNumber *badgeNumber = [payload objectForKey:@"badge"];
NSString *soundName = [payload objectForKey:@"sound"];
// app is in foreground
if (application.applicationState == UIApplicationStateActive) {
ALog(@"active");
if (badgeNumber) {
application.applicationIconBadgeNumber = [badgeNumber integerValue];
}
[self playPushNotificationAlertSound:soundName];
}
// app is in background
else if (application.applicationState == UIApplicationStateBackground) {
ALog(@"background");
if (badgeNumber) {
application.applicationIconBadgeNumber = [badgeNumber integerValue];
}
[self playPushNotificationAlertSound:soundName];
}
// app was launched from inactive state
else if ((application.applicationState == UIApplicationStateInactive)) {
ALog(@"inactive");
if (badgeNumber) {
application.applicationIconBadgeNumber = [badgeNumber integerValue];
}
}
}
- (void)playPushNotificationAlertSound:(NSString *)soundName
{
ALog(@"soundName = '%@'", soundName);
if (![soundName isEqualToString:@"default"]) {
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"caf"];
if (soundURL) {
CFURLRef soundFileURLRef = (__bridge CFURLRef)soundURL;
SystemSoundID soundFileObject = 0;
OSStatus status = AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
if (status == kAudioServicesNoError) {
AudioServicesPlayAlertSound(soundFileObject);
AudioServicesDisposeSystemSoundID(soundFileObject);
}
ALog(@"soundFileURLRef = %@", soundFileURLRef);
ALog(@"soundFileObject = %i, status = %i", (unsigned int)soundFileObject, (int)status);
}
}
}
这是没有活动断点且没有自定义声音播放的控制台日志:
2013-09-22 11:02:40.123 MyAppUnderDevelopment[2489:60b] push data package: {
"_" = BlyZ4SOYEeOEc5DiugJ6IA;
aps = {
alert = "Test #1";
badge = 1;
sound = moof;
};
}
2013-09-22 11:02:40.124 MyAppUnderDevelopment[2489:60b] active
2013-09-22 11:02:40.136 MyAppUnderDevelopment[2489:60b] soundName = 'moof'
2013-09-22 11:02:40.138 MyAppUnderDevelopment[2489:60b] soundFileURLRef = file:///var/mobile/Applications/31785684-2EFA-4FEB-95F3-3A6B82B16A4A/MyAppUnderDevelopment.app/moof.caf
2013-09-22 11:02:40.139 MyAppUnderDevelopment[2489:60b] soundFileObject = 4100, status = 0
控制台日志具有断点处于活动状态,单步执行且声音播放的功能:
2013-09-22 11:03:29.891 MyAppUnderDevelopment[2489:60b] push data package: {
"_" = "I_yGQCOYEeO515DiugJkgA";
aps = {
alert = "Test #2";
badge = 2;
sound = moof;
};
}
2013-09-22 11:03:29.892 MyAppUnderDevelopment[2489:60b] active
2013-09-22 11:03:33.752 MyAppUnderDevelopment[2489:60b] soundName = 'moof'
2013-09-22 11:03:40.757 MyAppUnderDevelopment[2489:60b] soundFileURLRef = file:///var/mobile/Applications/31785684-2EFA-4FEB-95F3-3A6B82B16A4A/MyAppUnderDevelopment.app/moof.caf
2013-09-22 11:03:45.356 MyAppUnderDevelopment[2489:60b] soundFileObject = 4101, status = 0
关于出什么问题有任何有用的想法吗?
在准备好问题之后,我将按照测试顺序进行操作,以确保正确记录了我所采取的步骤。 在快速浏览代码的同时,我发现调用...
AudioServicesPlayAlertSound(soundFileObject);
AudioServicesDisposeSystemSoundID(soundFileObject);
...以这种方式导致在异步声音有机会播放之前处理soundFileObject
。 短期解决方案是将soundFileObject变成保留的属性,并根据需要使用延迟实例化来创建它。
希望这可以帮助其他可能会撞墙的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.