ToneJS observe Player current time

477 views Asked by At

I've been trying to figure out how to observe the player's current time but without luck. I tried the immediate(), now(), and some other time-related API from the documentation.

https://tonejs.github.io/docs/14.7.77/Player.html

And also I tried several properties from the BaseContext. No luck so far.

Basically, I want to build a music player and display the current time played just like a regular music player. This should be possible for any audio library.

2

There are 2 answers

1
about14sheep On

Going through some of the github issue posts for tonejs for this, it appears adding this functionality will bring a lot of performance issues.

However, if you are simply wanting to get the amount of time the tone has been playing I don't see why you couldn't just save the start time in a variable and then subtract?

const startTime = Date.now();
const displayTime = Date.now() - startTime; // time in milliseconds

Save the startTime when you call the start method for the tone, then every render update the current time with displayTime. It is given in milliseconds, that you could then convert too mm:ss.

0
Artiphishle On

To get the position of the Tone.js player without the transport running, you can use the player.start method to schedule the playback and then query the position. Here's how you can do it:

  1. Create your player and connect it to the desired source.
  2. Schedule the player to start at the desired time using the start method.
  3. Query the player's position using the player.position property.

Here's some example code:

const Tone = require('tone');

// Create a player
const player = new Tone.Player('your-audio-file.mp3').toDestination();

// Schedule the player to start at a specific time (e.g., 0 seconds)
player.start(0);

// Query the player's position
const position = player.position;

// Log the position to the console
console.log('Player position:', position);

By scheduling the start of the player at the desired time, you can access its position without the transport running. The player.position property will give you the current position in seconds, even if the transport is stopped.