Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

audioSource.isPlaying returns false on indirect editor pause

Discussion in 'Editor & General Support' started by ArachnidAnimal, Apr 3, 2018.

  1. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    The audioSource.isPlaying is returning false on the same frame that the application is indirectly paused in the Editor. This is occurring when using Debug.Break() or when causing the Editor to loose focus.
    When manually pressing the pause button in the Editor, the issue does not occur.

    Using Unity 2017.1.1f1.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class testScript2 : MonoBehaviour {
    5.  
    6.     public AudioSource audioSource;
    7.     public bool doBreak;
    8.  
    9.     void Start()
    10.     {
    11.         audioSource.loop = true;
    12.         audioSource.Play();
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         if (doBreak) {
    18.             doBreak = false;
    19.             Debug.Break();
    20.         }
    21.  
    22.         if (!audioSource.isPlaying) {
    23.             Debug.LogError("audioSource reporting it is not playing");
    24.         }
    25.     }
    26. }
    27.  
    It sounds like a bug.
    Can anyone confirm?
     
    MaxLohMusic and JonBFS like this.
  2. millerlyte87

    millerlyte87

    Joined:
    Oct 31, 2014
    Posts:
    6
    I see the same thing in the editor.
    As a work around, I use MonoBeheavior.OnApplicationFocus to keep track if the game is in focus or not. If it's not in focus, do not check audioSource.isPlaying, and you should not see the error logs.
     
    MaxLohMusic likes this.
  3. glenneroo

    glenneroo

    Joined:
    Oct 27, 2016
    Posts:
    228
    Also running in to this, though it looks like it might be intentional, according to the docs for isPlaying:
    https://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html

    "Note: AudioSource.isPlaying will return false when AudioSource.Pause() is called. When you use AudioSource.Play() again back, it will return true."

    I guess when you pause in the Editor, it also calls AudioSource.Pause() internally :(

    millerlyte87 has a good suggestion, or you could either use a boolean manually store the actual isPlaying status, and only set to false when stopping the audio, or, set the volume to 0 when stopping and check that (assuming Pause() isn't being used).
     
    Last edited: Apr 6, 2021