如何在后台模式下停止音频?

时间:2017-10-02 17:31:44

标签: ios

我想在第一个控制器中以后台模式停止音频,并在第二个控制器中以后台模式播放音频。

我使用此代码:

NSString *soundFilePath = [NSString stringWithFormat:@"%@/%@.mp3",[[NSBundle mainBundle] resourcePath], @"sound"];
 NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
 _player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
 _player.numberOfLoops = 1000;
 [_player play];

要在后台模式中停止音频,请使用以下代码:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive: NO error: nil];

要在后台模式下播放音频,请使用以下代码:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

但我停止音频的代码不起作用......

UPD

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleForgroundScenario) name:UIApplicationWillEnterForegroundNotification object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleBackgroundScenario) name:UIApplicationWillResignActiveNotification object:nil];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

-(void) handleForgroundScenario {
    if([self.player rate] == 0) {
        [_player play];
    }
}

-(void) handleBackgroundScenario {
    [_player pause];
}

- (void)viewDidLoad
{
   [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    [[AVAudioSession sharedInstance]
     setCategory: AVAudioSessionCategoryPlayback
     error: nil];

    [super viewDidLoad];

         NSString *soundFilePath = [NSString stringWithFormat:@"%@/%@.mp3",[[NSBundle mainBundle] resourcePath], @"sound"];
         NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
         _player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
         _player.numberOfLoops = 1000;
         [_player play];
}

1 个答案:

答案 0 :(得分:0)

在ViewController的ViewWillAppear中添加ApplicationWillResign通知,您想在其中暂停播放器

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        NotificationCenter.default.addObserver(self, selector: #selector(handleBackgroundScenario), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
   }

暂停选择器中的播放器

@objc func handleBackgroundScenario() {
    //pause
}

最后删除viewWillDisappear中的观察者

override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self)
}

这将确保仅在加载ViewController且应用程序转到后台时才会触发通知。

对于所有其他VC通知不会触发选择器,因此它不会改变播放器的行为

修改

目标中的代码 - C

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleBackgroundScenario) name:UIApplicationWillResignActiveNotification object:nil];
}

-(void) handleBackgroundScenario {
   //pause player
   [_player pause];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

修改

  

应用程序变为活着后我应该在哪里呼叫玩家?

声明你的意思是当应用程序到达前台时启动播放器时你可以使用

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleForgroundScenario) name:UIApplicationDidBecomeActiveNotification object:nil];

最后实施handleForgroundScenario

-(void) handleForgroundScenario {
    if([self.player rate] == 0) {
        [_player play];
    }
}