I have audio playing issue on Android build. I\'m using Unity 5.4.0b15, but I had same issue on 5.3.4p3.
I have simple component for playing background music added t
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;
void Awake()
{
if (_musicAudioSource == null)
{
_musicAudioSource = gameObject.AddComponent();
_musicAudioSource.loop = true;
_musicAudioSource.clip = clip;
}
//Check if Audio is playing. Don't play if already playing.
if (!_musicAudioSource.isPlaying)
{
_musicAudioSource.Play();
}
}
void OnApplicationPause(bool pauseStatus)
{
//Check if this is Pause
if (pauseStatus)
{
//Pause Audio if it is playing
if (_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 Resume
if (!pauseStatus)
{
//Make sure audio is not null. If null, getComponent again
if (_musicAudioSource == null)
{
_musicAudioSource = gameObject.AddComponent();
_musicAudioSource.loop = true;
_musicAudioSource.clip = clip;
}
//Check if we paused the audio then resume
if (_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();
}
}
}