I have made a 2d game for android in Unity and there is an issue. There are sounds in the game (e.g. menu button sound) that work perfectly.
When I leave the game (e.g. with the home button) and come back, the sounds work well, too.
But if I get a sound notification (email, skpye etc), the game gets mute until I exit and restart the game.
I tried to search for the solution and I found OnApplicationPause method, but it doesn't help me. I don't know why...
Does someone have any idea what could be the problem or the solution :)?
Thanks.
EDIT
I connected my phone to the computer and tried to debug and follow the OnApplicationPause method.
The sound is playing when the game starts.
If I press the home button, the audioSource.Pause(); is called and when I'm back to the game the audioSource.UnPause(); is also called. And there is sound.
If I get a phone call, the audioSource.Pause(); is called and when I'm back to the game the audioSource.UnPause(); is also called. But there is no sound.
If I get a notifications (e.g. email) there is no audioSource.Pause() statement and the sound is gone.
The 'if (audioSource == null)' and the 'if (!audioSource.isPlaying)' statements are never called.
private AudioSource audioSource;
public AudioClip clip;
void Awake() {
if (audioSource == null) {
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.loop = true;
audioSource.clip = clip;
}
if (!audioSource.isPlaying) {
audioSource.Play();
}
}
void OnApplicationPause(bool pauseStatus) {
//Check if this is Pause
if (pauseStatus)
{
//Pause Audio if it is playing
if (audioSource.isPlaying)
{
audioSource.Pause();
//Set to true so that we will detamine whether to Play() or UnPause() the music next time
audioSourcePaused = true;
Debug.Log("ifplaying->pause");
}
Debug.Log("pause");
}
//Check if this is Resume
if (!pauseStatus) {
//Make sure audio is not null. If null, getComponent again
if (audioSource == null) {
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.loop = true;
audioSource.clip = clip;
Debug.Log("Null");
}
//Check if we paused the audio then resume
if (audioSourcePaused) {
audioSource.UnPause();
//Set to false so that we will detamine whether to Play() or UnPause() the music next time
audioSourcePaused = false;
Debug.Log("Unpause");
}
//Check if Audio is playing. Don't play if already playing.
if (!audioSource.isPlaying) {
audioSource.Play();
Debug.Log("play");
}
}
}