Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

iOS listen to music in the background while using Microphone.Start

Discussion in 'Scripting' started by marvpaul, Jun 20, 2021.

  1. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    I want to play background music on iOS (Spotify, Music app, any other app ...) and concurrently use the microphone as an input in Unity (using Microphone.Start).

    Has anyone achieved that in Unity for iOS?

    I can play music from the Music app and listen to another audio inside of the Unity app, thats working concurrently! But in the moment I initiate the microphone using Microphone.Start the background music stops. Also when the microphone was initialized and I start the background music again, in the moment I come back to the Unity app, the background audio will be muted.
    Interestingly enough, it works out of the box on Android.

    I found this Stackoverflow thread (https://stackoverflow.com/questions...-configuration-to-record-and-play-with-others) and tried to add
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error: nil];
    [[AVAudioSession sharedInstance] setActive:YES withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

    to my UnityAppController but I had no success with it. Someone in the Stackoverflow thread has suggested to set automaticallyConfiguresApplicationAudioSession = false; for the AVCaptureSession but I'm not even sure if I can access the AVCaptureSession Unity is using to record the microphone, so I don't know what else to do.

    Here's a video of this:


    Every help is really appreciated.
     
    Last edited: Jun 21, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    The first thing to find out is if iOS can even do this. Every time I do ANY application that uses the microphone, all audio is muted, so it might an iOS operating system assumption.

    Best way to figure it out would be to make a Swift app in XCode and see if you can pull off what you want, keeping music going when microphone is enabled. If you can't do it in Swift or ObjectionableC, you are unlikely to make Unity do it.

    They are completely unrelated operating systems using completely different approaches to manage shared resources.
     
  3. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    Thanks a lot for the answer!
    I know apps on iOS which can do that. I also know one which was made with Unity which is capable of doing this, the name of this application is STAELLA.
     
  4. Soulside

    Soulside

    Joined:
    Nov 1, 2018
    Posts:
    30
    Hey @marvpaul we run into the same issue. Have you figured out how to avoid stop background music to play when the microphone is used in Unity?
     
  5. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    @Soulside no unfortunately not yet. In case someone knows a solution, I would be really grateful if he can share.
     
  6. Soulside

    Soulside

    Joined:
    Nov 1, 2018
    Posts:
    30
  7. amirsouza1

    amirsouza1

    Joined:
    Oct 23, 2021
    Posts:
    1
    Olá, não sou desenvolvedor. Eu quero deixar uma sugestão.
    Muitos jogadores de Iphone têm um problema semelhante.
    Com o MIC no jogo (Chat Voice in-game), o IOS baixa automaticamente o volume e muda o áudio dos aplicativos de fundo para o áudio mono. Nos jogos, precisamos nos comunicar com a equipe via chat de voz (MIC ON). Precisamos de áudio estéreo para tocar, para ouvir as instruções (esquerda / direita). Quando você liga o MIC, o jogo baixa automaticamente o volume e muda para o áudio MONO. Impossível tocar com áudio baixo e mono. É um problema com muitos usuários. Seria possível criar um App para resolver esse problema? O aplicativo seria um sucesso, pois muitos usuários de gamer mobile do iPhone precisam de ajuda com este problema.
     
  8. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    @Soulside wow, thanks for that. Using NatDevice's microphone class is working. There's some kind of Audio crackeling which I need to investigate more, but in general I'm able to play music eg via Spotify and using the microphone inside of my app at the same time!
     
    Last edited: Nov 9, 2021
  9. Soulside

    Soulside

    Joined:
    Nov 1, 2018
    Posts:
    30
    Hey @marvpaul you wrote: "but in general I'm not able to play music eg via Spotify and using the microphone inside of my app at the same time!"

    Are you able, or not able to record the microphone when the background music is still playing?
     
  10. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    @Soulside sorry for this, I made a mistake while writing the text!
    When using NatDevice I'm able to play music (Spotify, iTunes ...) and using the microphone input inside of my app. So this problem is solved!
     
    Soulside likes this.