2

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");
        }
    }
}
dtamas80
  • 21
  • 1
  • 4

2 Answers2

0

With your updated code, you are doing it wrong. 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 then resume the music when pauseStatus is false. If you don't pause the audio, it will likely not play again after the system resumes.

You also have to check if the audiosource is null before playing or resuming the audio after pauseStatus is false.. Below is a complete sample that will get you started.

public AudioClip clip;
private AudioSource _musicAudioSource;
bool _musicAudioSourcePaused = false;

void Awake()
{
    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();
    }
}

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<AudioSource>();
            _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();
        }
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I have found this topic earlier. This time I totally copied this code, I set up a sound which starts to play when the game starts. And it works well. But when the external sound is 'arriving' the game sound disappears. – dtamas80 May 30 '16 at 17:22
  • I updated my previous comment, because I sent it by mistake. – dtamas80 May 30 '16 at 17:33
  • @dtamas80 the game sound will pause and will resume when the notification is out or when you are back to the game. Does that happen with the code above? Can you use music to verify if it pauses and resumes the music. Also make sure that you are running this exact code. – Programmer May 30 '16 at 17:40
  • When I left the game with the home button, the sound pauses and than resumes. (But this happend without this 'OnApplicationPause' method also). But if there is a notification, the sound stops and doesn't resume. I have to exit the game (not with the home button, I have to close the game totally) and restart again. The code is the same like in the example I just change the variables names. – dtamas80 May 30 '16 at 20:46
  • Any idea what should I try? – dtamas80 May 31 '16 at 22:46
  • @dtamas80 This works for me. Put Edit in your question and out your current code that is not working. I want to see if you are doing something wrong – Programmer Jun 01 '16 at 07:14
  • I have edited my post. (not only the code, there is more text also) – dtamas80 Jun 01 '16 at 09:38
0

Project Settings / Player / Other Settings / Mute Other Audio Sources*

Enjoy

Yolo
  • 1