Ios 当应用程序转到后台时暂停音频

Ios 当应用程序转到后台时暂停音频,ios,cocoa-touch,avaudioplayer,nsnotificationcenter,uiapplicationdelegate,Ios,Cocoa Touch,Avaudioplayer,Nsnotificationcenter,Uiapplicationdelegate,我有一个通知设置,在应用程序将退出活动状态时提醒我的VC: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseGame) name:UIApplicationWillResignActiveNotification

我有一个通知设置,在应用程序将退出活动状态时提醒我的VC:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pauseGame)
                                             name:UIApplicationWillResignActiveNotification
                                           object:[UIApplication sharedApplication]];
如您所见,它调用一个名为
pauseGame
的方法
除其他外,该方法包括:

[audio pauseAudio];
我在应用程序中使用相同的方法暂停,一切都正常工作

当应用程序被发送到后台时,会正确调用此方法,但是,当应用程序恢复时,音频将继续播放。其他暂停项正在进行恢复,因此该方法正在完成

进入背景时,如何使音频正确暂停

更新:人们一直在提到ApplicationIdentinterBackgroundNotification。我试过了,但效果一样。应该注意的是,苹果在AppDelegate中自己的评论中推荐了我最初使用的方法。请参见下文,了解他们在学员中的预先评论

    - (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
更新2:例如,使用WillResignActive可以在来电时正确暂停。在该事件中,音频会正确停止。因此,当按下home(主页)按钮时,应用程序似乎会进入后台。来电时不会调用后台通知,因此很明显,来电和home press会导致不同的状态

    - (void)applicationDidEnterBackground:(UIApplication *)application
 {
   /*
   Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
 If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
   */
    NSLog(@"Application moving to background");
  // Here Call your notification for pause  audio
}

试试这个方法

 -(void)applicationDidEnterBackground:(UIApplication *)application
     {

    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        }];

    }
还要在Appdelegate.h上声明bgTask

UIBackgroundTaskIdentifier bgTask;
在这里,您只需将通知中心更改为

[[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(pauseGame)
                                                     name: UIApplicationDidEnterBackgroundNotification
                                                   object: nil];

。希望这能帮助您……

在您的
视图中写下这篇文章:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayer) name:UIApplicationDidEnterBackgroundNotification object:nil];
并添加一个新方法:

-(void)pausePlayer{
    [audio pauseAudio];
}

有许多不同的通知,只需检查
ui应用程序。h

我发现停止音频与暂停音频可以解决问题。有趣的是,stop在恢复时不会重新启动音频,而是从停止的地方开始。这样一来,“停止”的作用与“暂停”的作用相同,满足了我的需要

我知道这是一个古老的问题,但我刚刚找到了同样问题的解决方案(2018年iOS 11仍然存在,我假设iOS 12也存在):

注册
AVAudioSessionInterruptionNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(avAudioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];
当应用程序恢复时,您将收到此通知。当你恢复应用程序时,你会被告知中断已开始,然后立即被告知中断已结束。如果正在播放音频,您将在“开始”处理程序中暂停它。然后,“结束”消息将没有“恢复”值,因此您应该停止音频。这是我的密码:

- (void) avAudioSessionInterruptionNotification:(NSNotification *)notification
    {
    AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if ( type == AVAudioSessionInterruptionTypeBegan )
        {
        // If your audio is active, pause it here.
        }
    else if ( type == AVAudioSessionInterruptionTypeEnded )
        {
        AVAudioSessionInterruptionOptions optionsValue = [notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue];
        if ( optionsValue == AVAudioSessionInterruptionOptionShouldResume )
            {
            // Resume your audio here
            }
        else
            {
            // Stop your audio here
            }
        }
    }

感谢您对格式进行的编辑。请尝试使用“UIApplicationIdentinterBackgroundNotification”-如下:[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(pauseGame)名称:UIApplicationIdentinterBackgroundNotification对象:nil];与使用我在上面发布的辞职声明的问题相同。我在发布的开始部分中已经做了很多。将通知更改为后台会产生与我在上面描述的问题相同的问题。苹果在AppDelegate文件中自己的评论建议使用我在原始帖子中使用的方法。但是,我尝试了后台方法很不幸,这并不能纠正这个问题。和我原来的帖子一样的问题。