簡體   English   中英

AudioServicesPlayAlertSound在應用程序期間未播放自定義聲音:didReceiveRemoteNotification:

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM