Access currentTime of sound (character has both animation and sound slots)

[premise: As the topic title implies, my obj has both animation and sound slot attached - looking for accessing the sound slot only … some threads treats time/duration etc by this.entity.model.animation or likewise … this.entity.sound is the tier in focus]

The ‘currentTime’ beneath SoundInstance | PlayCanvas API Reference
does not seem to align with: W3Schools Tryit Editor

  • I might have touched upon percentage of ‘sound.duration’ earlier but cannot find it in my history (nor by broad searches in the forum).

Here is the tried code:

[at initialize]
this.soundTogetCurrTime = this.entity.sound.slot("test"); console.log("stgs: ";

[at start of played sound]

... .playSound = function(){"test");   this.gct =  this.soundTogetCurrTime.currentTime;

[at update - with different tries]

var getCurTime ="test").currentTime; //Try 1: no autocompletion of 'source'
this.gct =  this.soundTogetCurrTime.duration; //Try 2: will console output as 'undefined'
    src=this.soundTogetCurrTime; this.gct2 = src.currentTime; //Try 3: try to access like the HTML example
    console.log("getCurTim: "+this.gct2); // will console output as 'undefined'

Do I really have to place the sound source out of PC-ressources? (cf the HTML-example, where the sound is placed as web-server source, and accessed by document.elementID).
No examples of currentTime syntax at API-references or in forum threads :-/

Hi @Thomas_Due_Nielsen,

Can you share a Playcanvas example that reproduces this issue to take a look?

So, if I understand correctly from your code, you are trying to get per frame the time remaining for the sound playing to finish?

I think you just need to grab a reference to the sound instance playing, not to the sound slot:

var TestSoundCurTime = pc.createScript('testSoundCurTime');

// initialize code called once per entity
TestSoundCurTime.prototype.initialize = function() {
    this.soundTogetCurrTime = this.entity.sound.slot("test"); 
    console.log("stgs: ";
    this.soundInstance = this.playSound();

// update code called every frame
TestSoundCurTime.prototype.update = function(dt) {
    console.log("getCurTim - duration: ", this.soundInstance.currentTime - this.soundTogetCurrTime.duration);

TestSoundCurTime.prototype.playSound = function(){
1 Like

Ok, an intial side remark (all with good a constructive mind/intention) in relation to the general usage and the knowledge of the total PC community (we all have different coding skillsets):

Mark up of landscape:

  1. The existing API reference (within the realm of the PC community webressources)
  2. The existing and ever expanding forum knowledge base [this included] (within the realm of the PC community webressources)
  3. The existing W3C knowledge base (not within the realm of the PC community webressources, but instead part of wide searchable knowledge of JS-coding)

A) It works! :slight_smile: and thanks a lot Leonidas.

B) A final side remark to the initial remark and to the community at large: So if I (alongside others) am/are to find similar solutions in the future, where to find sources like this.soundInstance? … where to look?
{As such: soundInstance seem to lay outside of the realms of 1), 2) and 3) - in a ‘realm 4)’ by itself. I guess it can be searched upon, but a google search within a developer oriented ‘github playcanvas soundinstance’ did not give me an example like Leonidas’ example above? … Is soundInstance knowledge from regular book sources?
the horizon of searching solutions can many times appear blurry/hazy for the tiers of developers with middle and lower coding skillset (a better cohesion between 1-4 could be helpful to many in here … will reference this topic at a Feedback topic as well)}

Regarding terminology the audio/sound API can be a bit difficult to grasp initially, the list of classes used is quite big:

  • Sound
  • SoundComponent
  • SoundInstance
  • SoundSlot
  • … then you have the Audio[name] classes as well to explore

But once you find your way around it, you have a lot of power and flexibility in your code. I can understand more tutorials/examples would be helpful here.

As far as sound instances, I checked the method and saw that it returns a pc.SoundInstance reference. The currentTime property is part of that class, so then it became straightforward how to use it.

Ok, I get the approach/thx again … I am getting better to use the API reference on daily basis (I guess)
[al though a bit contra-intuitive to name the accessible functionality-cluster ‘’ that sounds like a command → should be something like ‘pc.SoundComponent.playedSource’/have its own domain … but now when I know]

As I have seen that the Camera following a path | PlayCanvas Developer Site tutorial has been updated with a progression slider, the same could - to some extent - be done with a sound example.

1 Like