0

I have 1 controller. In first I start play audio1. I use this code:

- (void)viewDidLoad
{

     //code to not turn off audio with using mute switch

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

      //code to play audio

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

But when I hide my app audio not paused. I want to paused audio when I hide my app and play in pause point when app comes to foreground. How to do it?

Nishant Bhindi
  • 2,242
  • 8
  • 21
user
  • 11
  • 4
  • Possible duplicate of [Play/Pause with the same button \[AVAudioPlayer\]](https://stackoverflow.com/questions/6635604/play-pause-with-the-same-button-avaudioplayer) – kb920 Oct 04 '17 at 07:24

2 Answers2

2

For this you need to add notification for app enter in Background and foreground.

Declare player first

 AVAudioPlayer *player;

Add this code in ViewDidLoad

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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForegroundActive:) name:UIApplicationWillEnterForegroundNotification object:nil];

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

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

//code to play audio
NSString *soundFilePath = [NSString stringWithFormat:@"%@/%@.mp3",[[NSBundle mainBundle] resourcePath], @"alarm_tone"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1;
[player play];

Add Enter Background method.

-(void)appEnterBackgroundActive:(NSNotification*)note
{
     [player pause];
}

Add Enter Foreground method.

-(void)appEnterForegroundActive:(NSNotification*)note
{
    [player play];
}
Harshal Shah
  • 427
  • 3
  • 5
  • In next controller I use AVAudioSession and in this controller audio shouldn't stop if hide app. But If I use your code and hide app in next controller(when audio in next controller play and in first controller paused) and open audio from first controller will be playing together with audio from second controller –  user Oct 05 '17 at 16:09
0

You could use notification for detecting app will go background/foreground.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground:) name:@"enterBackGroundNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForeground:) name:@"enterForeGroundNotification" object:nil];

- (void)appEnterBackground:(NSNotification *) notification {
   [_player pause];
}

- (void)appEnterForeground:(NSNotification *) notification {
   [_player play];
}
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
  • In next controller I use AVAudioSession and in this controller audio shouldn't stop if hide app. But If I use your code and hide app in next controller(when audio in next controller play and in first controller paused) and open audio from first controller will be playing together with audio from second controller –  user Oct 05 '17 at 16:10