Search Unity

Audio: Pause/Resume doesn't work anymore (2021.3.0f1)

Discussion in 'Web' started by ManuBera, Apr 16, 2022.

  1. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Hey!

    After upgrading to 2021.3.0f1, my functionality of pausing and resuming audio playback isn't working anymore in WebGL builds. It's working in the editor and it has been working in WebGL when I was using version 2019.3.25f.

    I really need some help on this!

    Thanks!
    MB
     
  2. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    More specifically, it seems that setting AudioSource.time is not working.

    Edit: My mistake, setting AudioSource.time is working, but not when using Safari browser. Just using Pause and Play didn't work anymore with any browser.
     
    Last edited: Apr 18, 2022
  3. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    272
    Hi! We have some PRs with audio bug fixes in the queue, I'm checking if they are related to this specific issue and I'll report back!
     
    ManuBera likes this.
  4. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Thank you very much! I corrected my last reply, setting AudioSource.time seems to work with all browsers but Safari. Just using Play/Pause isn't working with any browser. Using UnPause didn't work either. I would love to have it work with any major browser, so help is still very much appreciated :)
     
  5. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
  6. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    45
    Hi,

    I just tried using AudioSource.Pause() and AudioSource.UnPause() in Unity 2021.3 and I wasn't able to reproduce the issue. Can you share you project or the script you are using?
    It would also be interesting to know what "Load Type" you are using for the audio clips("Decompress On Load" or "Compressed In Memory").

    Thanks,

    Marcel
     
    ManuBera likes this.
  7. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Thank you MarcelPursche! Have you tried it with Safari on an iOS device? I'm using "Compressed in memory" in this case, since the audio clips in question are very long (around 10 minutes).

    I can't really share the whole script, but I will try to make a truthful excerpt:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.     public class AudioController: MonoBehaviour
    7.     {
    8.         #region Enums
    9.  
    10.         public enum PlayBackState
    11.         {
    12.             Stopped,
    13.             Playing,
    14.             Paused
    15.         }
    16.  
    17.         #endregion
    18.  
    19.         #region Serialized Fields
    20.  
    21.         [SerializeField]
    22.         private AudioSource _audioSource;
    23.  
    24.         #endregion
    25.  
    26.         #region Fields
    27.  
    28.         private PlayBackState _playbackState = PlayBackState.Stopped;
    29.  
    30.         private AudioClip _audioClip;
    31.  
    32.         private Coroutine _waitForFinishRoutine;
    33.  
    34.         private float _audioSourceMarker;
    35.  
    36.         #endregion
    37.  
    38.         #region Properties
    39.  
    40.         public PlayBackState State => _playbackState;
    41.  
    42.         #endregion
    43.    
    44.         #region Public Methods
    45.  
    46.         public void Setup(AudioClip audioClip)
    47.         {
    48.             _audioClip = audioClip;
    49.  
    50.             _audioSource.clip = audioClip;                    
    51.         }
    52.  
    53.         public void Play()
    54.         {  
    55.             _audioSource.clip = _audioClip;
    56.  
    57.             if (_audioSourceMarker > 0f)
    58.             {
    59.                 _audioSource.UnPause();
    60.                 _audioSource.time = _audioSourceMarker;
    61.             }
    62.             else
    63.             {
    64.                 _audioSource.Play();
    65.             }
    66.  
    67.             _playbackState = PlayBackState.Playing;
    68.  
    69.             if (_waitForFinishRoutine != null)
    70.             {
    71.                 StopCoroutine(_waitForFinishRoutine);
    72.                 _waitForFinishRoutine = null;
    73.             }
    74.  
    75.             _waitForFinishRoutine = StartCoroutine(WaitForAudioFinish());
    76.         }
    77.  
    78.         public void Pause()
    79.         {
    80.             _audioSourceMarker = _audioSource.time;
    81.  
    82.             _audioSource.Pause();
    83.  
    84.             _playbackState = PlayBackState.Paused;
    85.         }
    86.  
    87.         public void Stop()
    88.         {
    89.             _audioSource.Stop();
    90.  
    91.             _playbackState = PlayBackState.Stopped;
    92.         }
    93.  
    94.         public void Unset()
    95.         {
    96.             _playbackState = PlayBackState.Stopped;
    97.  
    98.             _audioSource.clip = null;
    99.         }
    100.  
    101.         #endregion
    102.  
    103.         #region Private Methods
    104.  
    105.         private void OnPlaybackFinished()
    106.         {
    107.             _audioSource.Stop();
    108.  
    109.             _audioSource.time = 0f;
    110.  
    111.             _audioSourceMarker = 0f;
    112.  
    113.             Unset();
    114.         }
    115.  
    116.         private IEnumerator WaitForAudioFinish()
    117.         {
    118.             if (_audioSource == null || _audioSource.clip == null)
    119.                 yield return null;
    120.  
    121.             yield return new WaitUntil(() => {
    122.                 return _playbackState == PlayBackState.Playing && _audioSource.isPlaying == false;
    123.             });
    124.  
    125.             OnPlaybackFinished();
    126.         }
    127.     }
    128. }
    I hope that I haven't forgotten anything, but that should be the gist of it. The thing is, this code is already altered to have Play/Pause work on WebGL (except with Safari) again. When I switched version and it didn't work anymore, I didn't use the audioSourceMarker to remember where the audioclip was at pausing and I didn't use UnPause, but Pause and Play.

    Now I come to think of it, I guess the culprit could also be the Coroutine to check if the AudioClip has finished playing.

    Thank you very much!
     
    Last edited: Apr 20, 2022
  8. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    45
    Hi,
    I will investigate this further. It shouldn't be necessary to save and reset AudioSource.time for the pause function to work on WebGL. This should all be handled by the AudioSource Scripting API.
     
    ManuBera likes this.
  9. KalaivaniJ

    KalaivaniJ

    Joined:
    Oct 13, 2022
    Posts:
    2
    Any update on this, I'm struck as well.
     
  10. KalaivaniJ

    KalaivaniJ

    Joined:
    Oct 13, 2022
    Posts:
    2
    It is working in unity 2022.2.1f1
     
  11. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    I'm experiencing the same issue.

    AudioSource.Pause() and AudioSource.UnPause() works in editor but does not work in WebGL.

    In WebGL, when we press pause, instead of pausing it appears to mute the audio and when we UnPause it resumes from a different point.

    I'm using Unity 2021.3.22f1
     
  12. Voodoodoo_Studio

    Voodoodoo_Studio

    Joined:
    Mar 8, 2023
    Posts:
    2
    Also having the same issue still
     
  13. Voodoodoo_Studio

    Voodoodoo_Studio

    Joined:
    Mar 8, 2023
    Posts:
    2
    I didn't find a fix but i found a simple workaround to avoid using the Pause() Unpause() methods by saving the time the audio is on when it is "paused" on in a variable and playing the audio from the beginning. It actually stops the audio and starts from the beginning but it instantly skips to the time it was on when it was "paused".
    Code (CSharp):
    1.  
    2.  private AudioSource music;
    3.  private float currentTime;
    4.  
    5.  public void TogglePauseMusic()
    6.     {
    7.         if (!music.isPlaying)
    8.         {
    9.             music.Play();
    10.             music.time = currentTime;
    11.         }
    12.         else
    13.         {
    14.             currentTime = music.time;
    15.             music.Stop();
    16.         }
    17.     }
     
    ElMephisto likes this.
  14. ElMephisto

    ElMephisto

    Joined:
    Jan 27, 2019
    Posts:
    1
    Thank you so much, this saved my day!
    I was thinking there's no way the pause() and unpause() methods would be causing issues in WebGL