The UILocalNotification sound does not stop playing

前端 未结 1 431
猫巷女王i
猫巷女王i 2020-12-21 05:41

I have set a custom .aif 30 second file as the local notification sound name. And below is my code for scheduling the local notification.

//Function to sched         


        
相关标签:
1条回答
  • 2020-12-21 06:39

    This appears to be an open bug with iOS 7 as filed here. It also seems that when a device is passcode-locked, this issue does not appear. A pretty ugly hack that worked for me is setting a value for the application badge number and removing it immediately when the app comes into foreground. A sample code would be:

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    
    }
    

    EDIT:

    Apparently, the above mentioned hack is not actually working on iOS 7.1+. The only work-around I found is the following, but I'm very hesitant in calling it an actual answer:

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
       AVAudioSession *session = [AVAudioSession sharedInstance];
       [session setCategory:AVAudioSessionCategoryPlayback error:nil];
       [session setActive:YES error:nil];
       MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
        [musicPlayer setVolume:0.0f]; 
    }
    

    But there are a number of serious flaws with the code above:

    1. setVolume method is deprecated since iOS 7 (although apparently many apps are still using it)
    2. You have at some point of your app to re-set the volume to a proper (non-zero level)
    3. Setting the volume property will most definitely have side-effects in other apps that might be playing sounds or music at the same time

    UPDATE September 18, 2014

    This issue seems to be resolved on iOS 8.

    0 讨论(0)
提交回复
热议问题