Current accurate time of an audio clip

Hi,

I would like to know the accurate current time( every frame) of an audio clip being played.

  1. I tried audio.time but it doesn’t seems to work and stay synchronized with sound. For example, when you click in unity editor on menu to change aspect ratio, the game is freezed and the music paused. And when you release the mouse click, the music is resumed at the same position, but the audio.time is totally desynchronized.

  2. I tried to use timeSamples with float time = audio.timeSamples/audio.clip.frequency; but the timeSamples value is updated only every seconds, so it’s useless for what I need.

Any idea?

Thanks,
Laurent

As I have no asnwers since 2 weeks, I try again. Any idea?
How can I have accurate audio time or sample?

audio.time.

I know you said it “seems” not to work, but what “seems” wrong?

As far as the time off synching when you change the aspect ratio, this is something you would never do in game, so why is it an issue?

Thanks for your reply.

Actually it also happens with the builded game:

  • in windowed mode on windows, each time you drag the window.
  • in fullscreen mode, each time you alt tab, press window key…

When you do one of these actions, the “autio.time” is totally different from real current time of audio clip. Audio.time is a sort of elapsed time, and it never stops, even when music stops.

This is very annoying because I am working on a karaoke game, and I need to kepp lyrics and audio synchronized.

For the moment I use a trick, I have my own time that I increment every Update, but only when difference with previous time is less than the duration of 5 frames. The result is a bit better than audio.time, but it’s far from perfect, I still have desync problems.