Assistance using setTimelinePosition(...) in Unity, StudioEventEmitter

I am using Unity and FMOD 2.01.08 in Unity, and have also tried 2.01.07.

I am attempting to get streamed music tracks set up in FMOD Studio to resume playing from where they are left off.

To do this:

  • I have added a GetSeekPosition() method to StudioEventEmitter:
public int GetSeekPosition()
{
    if (instance.isValid())
    {
        int pos;
        instance.getTimelinePosition(out pos);
        return pos;
    }
    return 0;
}
  • I have updated StudioEventEmitter.PlayInstance() to include the following code immediately after instance.start(). I have also tried it immediately before instance.start().
    if (seekPosition > 0) // Added by RT
    {
        FMOD.RESULT result = instance.setTimelinePosition(seekPosition);
        Debug.Log(result);
    }
  • In my own class, when I stop a track I call GetTimelinePosition() and store the value in a dictionary. When starting a track, if there is a stored value I pass it in via the above code. The logged result from this shows “OK”. I am logging the times I am storing and passing back and these all match what I expect.

Unfortunately, the music always starts from the start of the track. The tracks are all set to stream. If I disable streaming I get a silent period equal to how far the seek was meant to be.

It seems to me like the value is getting passed in successfully. Could someone please point me towards any other steps I may be missing?

Assuming I get this to work, will it honor the AHDSR volume fader I have attached on the music tracks? If not I guess the solution is to make one StudioEventEmitter per track.

Thanks very much!

Oh! I’m also changing the Event on the StudioEventEmitter. Not at my PC to copy the code, but basically change the string, call Stop() and then LookUp(). That all happens before the setTimelinePosition().

I also tried using two StudioEventEmitters, one for the starting sound and one for the stopping sound, which fades out. Same results either way.

I think tomorrow I’ll change it to create one emitter per track do they don’t get changed once made, see if that helps. That would also allow me to pause instead of start/stop (edit: wait, no, that breaks the fade).

This didn’t end up changing anything. Behaviour is exactly the same as before.

We have a number of sounds in the game where being able to seek is important, so I need to get this figured out.

Edit: More experimentation. Just using getTimelinePosition(), adding 5000ms, and putting it back into setTimelinePosition() had no effect at all. So I converted the events from using “Action Sheets” to using “Timeline Sheets” and now I can at least skip forward using that little test function. Still no effect when trying to seek immediately after starting an effect, still working on that.

Well, alright then. I made a new Unity project, a new FMOD project, re-implemented an equivalent thing, and it worked. Sweet. :slight_smile:

I then brought over the scripts from my main project and they also work in that new project. So that’s going to be fun to sort out, but at least the FMOD API is working as expected.

So, music events from the new FMOD project work as expected in either of my Unity projects. Events from the old FMOD project misbehave in both of my Unity projects.

The new FMOD project is a copy of the examples with just two new events added with a music track each. So, best guess is that I made some rookie mistake when setting up my own blank FMOD project.

Richard, thank you for the commentary on this issue. We will be getting to playing music from the last stop position in a few weeks. Can you post your finished code for others to follow what you ended up with.
Thank you.

The issue turned out to be something with the FMOD Studio project, not the code itself. I’m still not 100% sure what, but by making a new FMOD Studio project I had music playing from a set timeline position in my main project without making any code changes there, and without any of the issues I was originally happening.

I did grab an older version of FMOD Studio (2.00.16) at some point when I was trying different things. No idea if that is related. (Edit: Not related. I deleted my music events in the original 2.01.08 project, re-created them, and now everything is groovy there, too.)

In my test project this is the simple test music player I wrote which switches between two tracks:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;

public class TestMusicPlayer : MonoBehaviour
{

    StudioEventEmitter loopEmitter1 = null;
    StudioEventEmitter loopEmitter2 = null;
    StudioEventEmitter currentEmitter = null;

    int loop1Pos = 0;
    int loop2Pos = 0;

    // Start is called before the first frame update
    void Start()
    {

        loopEmitter1 = gameObject.AddComponent<StudioEventEmitter>();
        loopEmitter1.Event = "event:/Music/LoopTest1";
        loopEmitter2 = gameObject.AddComponent<StudioEventEmitter>();
        loopEmitter2.Event = "event:/Music/LoopTest2";

    }

    // Update is called once per frame
    void Update()

    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SwapMusics();
        }
    }

    void SwapMusics()
    {
        if (currentEmitter != null)
        {
            if (currentEmitter == loopEmitter1)
            {

                currentEmitter.EventInstance.getTimelinePosition(out loop1Pos);
            }
            else
            {
                currentEmitter.EventInstance.getTimelinePosition(out loop2Pos);
            }
            currentEmitter.Stop();
        }

        int pos = 0;
        if (currentEmitter == loopEmitter1)
        {
            currentEmitter = loopEmitter2;
            pos = loop2Pos;
        }
        else
        {
            currentEmitter = loopEmitter1;
            pos = loop1Pos;
        }

        currentEmitter.Play();

        currentEmitter.EventInstance.setTimelinePosition(pos);
    }
}