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:
[SerializeField] private AudioClip mySound = null;
private AudioSource sfxAudioSource;
private void Awake()
{
this.sfxAudioSource = this.gameObject.AddComponent<AudioSource>();
}
private void Start()
{
this.sfxAudioSource.volume = 1f;
Button button = GameObject.Find("Canvas").transform.Find("TestButton").GetComponent<Button>();
button.onClick.AddListener(this.PlayMySound);
}
private void PlayMySound()
{
this.sfxAudioSource.PlayOneShot(this.mySound);
}
Can anybody else confirm this?