iOS Question How to let Music play in background while app makes sounds

Discussion in 'iOS Questions' started by Turbo3, Feb 21, 2015.

  1. Turbo3

    Turbo3 Active Member Licensed User

    Since B4i does not support the B4A Beep function I needed to use Mediaplayer to make sounds.

    My TestFlight testers have reported that when my app makes sounds the music they were playing in the background stops.

    How can I allow background music and still make sounds in the app?

    Or if the music must stop while the app makes a sound how can my app restart the background music they were previously playing?

    As a quick fix, for now I have added a setting to disable the app from making any sounds.
     
    Haj_rasool likes this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    #If OBJC
    - (void) setAudioSession {
       [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    }
    #End If
    Call it when your app starts:
    Code:
    Dim nativeMe As NativeObject = Me
    nativeMe.RunMethod(
    "setAudioSession"Null)
     
    Haj_rasool likes this.
  3. Turbo3

    Turbo3 Active Member Licensed User

    Did not work. I already have a routine called setAudioSession so I renamed this new one to setAudioSession1. Here is the code I already had:
    Code:
    #If OBJC
    #import <AVFoundation/AVFoundation.h>
    - (void) setAudioSession {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        BOOL ok;
        NSError *setCategoryError = nil;
        ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
          error:&setCategoryError];
        if (!ok) {
              NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
        }
    }
    #end if
    Do I need to combine the two routines into one?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Does it work if you only set the ambient parameter?
     
  5. Turbo3

    Turbo3 Active Member Licensed User

    Actually I switched the order of execution so the new code was executed last and it works but the old code that disabled looking at the ringer silence switch not longer works.

    So it looks like we need to combine these two into one routine.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    That is correct.

    Use this code:
    Code:
    #If OBJC
    #import <AVFoundation/AVFoundation.h>
    - (void) setAudioSession {
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      BOOL ok;
      NSError *setCategoryError = nil;
      ok = [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers
      error:&setCategoryError];
      if (!ok) {
      NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
      }
    }
    #end if
     
  7. Turbo3

    Turbo3 Active Member Licensed User

    Erel, That did it! Thank you very much. Jim
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice