Search Unity

Feedback IsPaused property for AudioSource is needed.

Discussion in 'Scripting' started by Deleted User, Mar 5, 2019.

  1. Deleted User

    Deleted User

    Guest

    AudioSource has Pause() and UnPause() functions but there is no anything like public property which could indicate whether that AudioSource is paused. Sometimes the property is necessary but I have to check the pause state by other unreliable ways.
     
    nco2k likes this.
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
  3. Deleted User

    Deleted User

    Guest

    It doesn't indicate whether AudioSource is paused or stopped completely.
    Yes, I have to use the isPlaying property doing something like this:

    Code (CSharp):
    1. public static bool IsPaused(this AudioSource source)
    2. {
    3.     return !(source.isPlaying || source.time == 0f);
    4. }
    But as I said this way is unreliable, because in rare cases you can call Pause() function when AudioSource time equals zero. Technically it will be paused but you will not be able to distinguish the states.
     
    nco2k likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Or...just keep your own ispaused bool variable? Hit pause, set bool to true, hit unpause, set it to false.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    For many applications, this chance may be small enough that you can ignore it. floats are very rarely equal; there's about a billion floating-point values between 0 and 1.

    I think you are missing the point.

    Any
    piece of information based on your program's history can be made available if you track it yourself. For instance, if there was no isPlaying property, you could track all of your calls to play, stop, pause, etc. so that you always know whether it "should" be playing.

    But the AudioSource clearly needs to track this information anyway, so you'd be doing a bunch of redundant work just to compensate for an obvious gap in its interface. And if there's a possibility that someone else's code makes any calls to the AudioSource--or, heaven forbid, if there's a bug in the AudioSource--then your answer of whether the AudioSource "should" be playing might still be different from what the AudioSource thinks it is doing. So even after you've put in all of that extra work, this is still a worse solution.

    I also think you are overlooking the fact that your most recent proposed solution (adding up time the AudioSource is playing) wouldn't work unless you were already tracking all of this anyway. You can't do it by polling: if last frame the isPlaying was true and your accumulated time was 3, but this frame isPlaying is false and the time is zero, that could mean that the AudioSource was stopped OR that it looped and got paused on zero. The only way this approach helps is if you already "know" all of the pause/play/stop calls being made and can track them directly, so you were already implicitly assuming that you could do that.
     
  6. nco2k

    nco2k

    Joined:
    Apr 30, 2018
    Posts:
    6
    it really blows my mind that unity is missing such a basic feature. yes, we could keep track of it ourselves, but why? unity already keeps track of it for us, which is why source.UnPause() only works if the sound has been previously started and paused. exposing this parameter should only take a couple of seconds, and i absolutely cannot understand why the team is being so against it.