Unity3d - Audio Playing Getting Muted After Getting Any Call/notification On Android
Solution 1:
This is definitely a bug in Unity. I bumped into the same problem and ran some tests to try to fix it but there is no way to recover the audio without restarting the app.
In my tests I used Google Hangouts notifications to kill the audio.
Programmer's proposed fix won't work because: 1. The notification does not trigger application pause nor application focus events. 2. Initializing a new AudioListener and/or AudioSource instances will not recover the audio either.
This is a low level bug in Unity. I will make a report on the bug.
Solution 2:
On iOS everything works perfectly.
Android != iOS
.
Use OnApplicationPause(bool pauseStatus)
to detect when the Application is paused and resumed. The pauseStatus
variable will return true
when the app is paused, and false
when the app is resumed. So, manually pause the the music when pauseStatus
is true
and resume the music when pauseStatus
is false
.
public AudioClip clip;
private AudioSource _musicAudioSource;
bool _musicAudioSourcePaused = false;
voidAwake()
{
if (_musicAudioSource == null)
{
_musicAudioSource = gameObject.AddComponent<AudioSource>();
_musicAudioSource.loop = true;
_musicAudioSource.clip = clip;
}
//Check if Audio is playing. Don't play if already playing. if (!_musicAudioSource.isPlaying)
{
_musicAudioSource.Play();
}
}
voidOnApplicationPause(bool pauseStatus)
{
//Check if this is Pauseif (pauseStatus)
{
//Pause Audio if it is playingif (_musicAudioSource.isPlaying)
{
_musicAudioSource.Pause();
//Set to true so that we will detamine whether to Play() or UnPause() the music next time
_musicAudioSourcePaused = true;
}
}
//Check if this is Resumeif (!pauseStatus)
{
//Make sure audio is not null. If null, getComponent againif (_musicAudioSource == null)
{
_musicAudioSource = gameObject.AddComponent<AudioSource>();
_musicAudioSource.loop = true;
_musicAudioSource.clip = clip;
}
//Check if we paused the audio then resumeif (_musicAudioSourcePaused)
{
_musicAudioSource.UnPause();
//Set to false so that we will detamine whether to Play() or UnPause() the music next time
_musicAudioSourcePaused = false;
}
//Check if Audio is playing. Don't play if already playing. if (!_musicAudioSource.isPlaying)
{
_musicAudioSource.Play();
}
}
}
Post a Comment for "Unity3d - Audio Playing Getting Muted After Getting Any Call/notification On Android"