该UILocalNotification声音不停止播放 [英] The UILocalNotification sound does not stop playing

查看:194
本文介绍了该UILocalNotification声音不停止播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了自定义.AIF30秒文件作为本地通知声音名称。以下是我的code调度本地通知。

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 schedule local notification
-(void)schedulelocalnotification:(NSDate *)particularfiredate ringtone: (NSString *)particularringtone name:(NSString *)alarmname info:(NSDictionary *)dicttext
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = particularfiredate;
notification.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
notification.alertBody = alarmname;
notification.userInfo = dicttext;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

但是,当在设备锁定,并在该通知的用户滑动输入的应用程序,声音不断甚至当用户进入该应用播放。它继续甚至当用户退出/卸载应用程序播放。
请建议可能是什么可能的原因。

But when the device is locked, and the user slides on the notification to enter the app, the sound keeps on playing even when the user enters the app. It continues to play even when the user quits/uninstalls the app. Please suggest what could be the possible reasons.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

    [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"demo"];
    NSLog(@"%i",[[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
    NSString *viewcontrollerstring = [notification.userInfo objectForKey:@"smiletosnooze"];
    NSLog(@"++++++------%@",viewcontrollerstring);


}

PS:我查 - <一个href=\"http://stackoverflow.com/questions/10143440/uilocalnotification-stop-sound-after-notification-is-dismissed\">UILocalNotification停止声音通知被驳回并在此之后 - <一个href=\"http://stackoverflow.com/questions/24340423/stop-uilocalnotification-sound-on-slide-to-view\">Stop UILocalNotification声音幻灯片查看,但它是没有帮助。 (

PS: I checked - UILocalNotification stop sound after notification is dismissed and this - Stop UILocalNotification Sound on slide to view but it was of no help. :(

推荐答案

本所提交的此处。这似乎也当设备通code-锁定,这个问题不会出现。一个pretty丑陋的黑客攻击,对我的工作是设置应用程序的徽章数量的值,当应用程序进入前台立即删除它。样本code将是:

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];

}

修改

显然,上述黑客并不实际工作的iOS 7.1或更高版本。我发现的唯一的,周围的工作是以下,但我称它实际的答案我很犹豫:

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]; 
}

不过,也有一些与上面的code严重的缺陷:

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


  1. setVolume 法德$ P $,因为iOS的7 pcated(虽然看起来很多应用仍然使用它)

  2. 您必须在您的应用程序的某个时间点重新设置音量到合适的(非零级)

  3. 设置音量属性肯定会在其他应用程序的副作用可能被播放声音或音乐的同时

  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

更新2014年9月18日

此问题似乎在iOS 8有待解决。

This issue seems to be resolved on iOS 8.

这篇关于该UILocalNotification声音不停止播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆