Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Android: Targeting API 31 causes audio to get muted forever after losing app focus

Discussion in 'Editor & General Support' started by msnz, Oct 11, 2022.

  1. msnz

    msnz

    Joined:
    Nov 4, 2020
    Posts:
    31
    When I set the max Target API Level to 31, everything seems to work fine. Sounds play as normal on devices with Android version 11 and below.

    However, when I try playing on an device that has Android version 12, the sounds still play as normal, but once the app loses focus (minimized) and is then brought back to focus again, I can no longer hear any of the sounds playing, no matter how many times I click on the button.

    A quick solution is to enable "Mute Other Audio Sources" in the Settings. That seems to get rid of the issue. However, this is not ideal, as I would like players to still be able to play music from another app while playing the game.

    I have tested this on a clean project with just a button that triggers the sounds.
    Unity version is: 2020.3.35f1
    Min Target API: Level 27, Max Target API: Level 31
    Test device: Samsung A22-5G

    The code I'm using to play sounds is nothing special:

    Code (CSharp):
    1. [SerializeField] private AudioClip mySound = null;
    2. private AudioSource sfxAudioSource;
    3.  
    4. private void Awake()
    5. {
    6.     this.sfxAudioSource = this.gameObject.AddComponent<AudioSource>();
    7. }
    8.  
    9. private void Start()
    10. {
    11.     this.sfxAudioSource.volume = 1f;
    12.  
    13.     Button button = GameObject.Find("Canvas").transform.Find("TestButton").GetComponent<Button>();
    14.     button.onClick.AddListener(this.PlayMySound);
    15. }
    16.  
    17. private void PlayMySound()
    18. {
    19.     this.sfxAudioSource.PlayOneShot(this.mySound);
    20. }
    Can anybody else confirm this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    msnz likes this.
  3. msnz

    msnz

    Joined:
    Nov 4, 2020
    Posts:
    31
    Thanks. That was the issue. Updated Unity to 2020.3.40 and the problem went away.
     
    Kurt-Dekker likes this.