我希望其他人能深入了解这个问题......
我正在尝试播放加载到我的应用程序资源中的自定义声音。我使用 afconvert 工具创建了声音,如使用 OpenAL线程将音频转换为CAF 格式以在 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
关于出了什么问题的任何有用的想法?