# Some delay occurs when pause the music on Android

**URL:** <https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247>\
**Category:** Unity\
**Tags:** android\
**Created:** [September 7, 2022, 4:30pm UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247 "2022-09-07T16:30:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceuser](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@iceuser](https://qa.fmod.com/u/iceuser)\
**Post date:** [September 7, 2022, 4:30pm UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247/1 "2022-09-07T16:30:21Z")

</div>

I am developing a rhythm game for Android.

Other features work great and better than Unity native audiosystem.

But, I have a problem in implementing the pause and resume function.

I implemented the pause and resume function using channel.setPause(),  
and found that there was a slight delay when resume using channel.setPause(false).

[https://qa.fmod.com/t/off-sync-on-position-gettimelineposition-when-using-setpaused-method/13069/2](https://qa.fmod.com/t/off-sync-on-position-gettimelineposition-when-using-setpaused-method/13069/2)  
I saw in one article that it need to sync using dspclock, but I don’t understand the solution well, so I can’t solve this problem.

Is there anyway this problem can be solved?

Thank you.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [September 9, 2022, 1:41am UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247/2 "2022-09-09T01:41:54Z")

</div>

Hi,

So DSP clock is the correct solution to the problem. I’ll try to explain it in a different way.

First, we will need the `Channel` and `Channel Group` to which the event belongs. I achieved this like so:

> **Retrieving Channel and Channel Group**
>
> ```auto
> /// <summary>
> /// Using supplied event Emitter return the event's channel and channel group
> /// </summary>
> private FMOD.RESULT ReturnEventChannel(FMODUnity.StudioEventEmitter eventEmitter, out FMOD.Channel requestedChannel, out FMOD.ChannelGroup channelsGroup)
> {
> requestedChannel = new FMOD.Channel();
> channelsGroup = new FMOD.ChannelGroup();
> 
> FMOD.RESULT result = eventEmitter.EventInstance.getChannelGroup(out FMOD.ChannelGroup newgroup);
> 
> //Checking at every step if there is an error which will return a invalid result 
> if (result != FMOD.RESULT.OK)
> {
> Debug.Log("Failed to retrieeve parent channel group with error: " + result);
> return result; 
> }
> 
> result = newgroup.getGroup(0, out channelsGroup);
> 
> if (result != FMOD.RESULT.OK)
> {
> Debug.Log("Failed to retrieve child channel group with error: " + result);
> return result;
> }
> 
> result = channelsGroup.getChannel(0, out requestedChannel); 
> 
> if (result == FMOD.RESULT.OK)
> {
> Debug.Log("Retrieved the correct channel"); 
> return result;
> }
> else
> {
> Debug.Log("Failed to find the right channel with error: " + result);
> return result;
> }
> }
> 
> ```

Now when you pause the event, get the current DSP position. To do so we need the `Channel Group` the event belongs to. The `Channel Group` is an `out` value of the previous function. So we can just use that.

> **Getting current DSP clock**
>
> ```auto
> private FMOD.RESULT GetCurrentDSPClock(FMOD.ChannelGroup cG, out ulong dspClock, out ulong parent)
> {
> FMOD.RESULT result = cG.getDSPClock(out dspClock, out parent);
> 
> if (result != FMOD.RESULT.OK)
> {
> Debug.Log("Failed to retrieve DSP clock with error: " + result);
> dspClock = 0;
> parent = 0;
> return result;
> }
> else
> Debug.Log("Retrieved DSP Clock"); 
> 	
> return result; 
> }
> 
> ```

Now we have an accurate value to set our playback position to when we resume the event avoiding any drift.

```auto

channelToPause.setPosition((uint)dspClockCurrent, FMOD.TIMEUNIT.PCM);

channelToPause.setPaused(false);

```

Hope this explains it better!

\*Removed a member bool which would cause errors

---

<div class="post-metadata">

**Author:** ![iceuser](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@iceuser](https://qa.fmod.com/u/iceuser)\
**Post date:** [September 9, 2022, 4:54am UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247/3 "2022-09-09T04:54:54Z")

</div>

I’m using Core System right now,  
and when I used channelGroup.getDSPClock() to get dspClock,  
it seems to work like uptime.  
If I apply this as a setPosition(), it goes against the actual music. What am I using wrong?

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [September 12, 2022, 12:23am UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247/4 "2022-09-12T00:23:06Z")

</div>

Hi,

Is it possible to get a code snippet of how you are trying to retrieve the DSP clock and then use it to set the playback position?

---

<div class="post-metadata">

**Author:** ![iceuser](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@iceuser](https://qa.fmod.com/u/iceuser)\
**Post date:** [September 14, 2022, 1:44pm UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247/5 "2022-09-14T13:44:08Z")

</div>

Sorry, I wrote the wrong code. Now it works perfectly. Thanks!

---

<div class="post-metadata">

**Author:** ![brettridel](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@brettridel](https://qa.fmod.com/u/brettridel)\
**Post date:** [December 9, 2024, 11:07pm UTC](https://qa.fmod.com/t/some-delay-occurs-when-pause-the-music-on-android/19247/6 "2024-12-09T23:07:04Z")

</div>

Hey [iceuser](https://qa.fmod.com/u/iceuser), may you share the pause/unpause code ?
