简体   繁体   中英

Sound issue in an android game made in Unity

I have made a 2d game for android in Unity and there is an issue. There are sounds in the game (eg menu button sound) that work perfectly.

When I leave the game (eg 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 (eg 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");
        }
    }
}

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

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

Enjoy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM