首页 文章

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

提问于
浏览
0

当应用程序要重新启动活动状态时,我有一个通知设置来提醒我的VC:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pauseGame)
                                             name:UIApplicationWillResignActiveNotification
                                           object:[UIApplication sharedApplication]];

正如您所看到的,它调用了一个名为 pauseGame 的方法 .
除其他外,该方法包含:

[audio pauseAudio];

我在应用程序中使用相同的方法暂停,一切正常 .

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

如何在进入后台时让音频正常暂停?

更新:人们一直在提及ApplicationDidEnterBackGroundNotification . 我尝试过,但它的工作方式相同 . 值得注意的是,Apple在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正常工作,例如在呼叫进入时暂停 . 通过该事件,音频可以正常停止 . 因此,当按下主页按钮时,似乎只是应用程序进入后台 . 没有为来电呼叫后台通知,因此显然呼叫和主页按下会导致不同的状态 .

5 回答

  • 1
    - (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
    }
    

    https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

  • 0

    试试这个......

    -(void)applicationDidEnterBackground:(UIApplication *)application
         {
    
        bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    
                [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            }];
    
        }
    

    同时在Appdelegate.h上声明bgTask

    UIBackgroundTaskIdentifier bgTask;
    

    在这里,您只需将NSNotifcation Center更改为

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

    . 希望对你有帮助....

  • 0

    在_2918746中写下这个:

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

    并添加一个新方法:

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

    有很多不同的通知,只需检查 UIApplication.h

  • 0

    我发现停止音频而不是暂停它会处理问题 . 值得注意的是,停止不会在恢复时启动音频,而是从停止的位置开始 . 这样,stop就像暂停一样工作,满足了我的需求 .

  • 0

    我知道这是一个古老的问题,但我刚刚找到了解决同样问题的解决方案(在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
                }
            }
        }
    

相关问题