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

时间:2014-05-07 05:53:10

标签: ios cocoa-touch avaudioplayer nsnotificationcenter uiapplicationdelegate

当应用程序要重新启动活动状态时,我有一个通知设置来提醒我的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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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];

。希望这会对你有帮助....

答案 2 :(得分:0)

将此内容写入viewDidLoad

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

并添加一个新方法:

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

有许多不同的通知,只需查看UIApplication.h

即可

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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