AudioServicesPlayAlertSound在应用程序期间不播放自定义声音:didReceiveRemoteNotification:

时间:2013-09-22 15:29:38

标签: iphone ios audio

我希望其他人能够深入了解这个问题...

我正在尝试播放加载到我应用资源中的自定义声音。我使用afconvert工具创建了声音,如将音频转换为CAF format for playback on iPhone using OpenAL线程所建议的那样。为生成我的自定义声音文件而执行的特定命令是:

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

关于出了什么问题的任何有用的想法?

1 个答案:

答案 0 :(得分:1)

在准备好我的问题之后,我正在测试我的测试序列,以确保我已经正确记录了我所采取的步骤。虽然单步快速通过代码,我发现调用...

            AudioServicesPlayAlertSound(soundFileObject);
            AudioServicesDisposeSystemSoundID(soundFileObject);

...以这种方式导致soundFileObject在异步声音有机会播放之前被释放。短期解决方案是将soundFileObject转换为保留属性,并使用延迟实例化根据需要创建它。

希望这可以帮助那些可能正撞墙的人。