Please correct the errors below.
L/R L/R

Getting player data via the JavaScript API

Getting data from the Soundslice player is a bit involved, due to the way postMessage() works. In each case here, you post a message to the iframe, then listen for the appropriate event response and get its arg.

Note you should set up event listeners before calling postMessage(), to avoid a possible race condition where the iframe sends you a message before you’re listening for it.

Here is a full example, using current time:

// Handle inbound current time messages.
window.addEventListener('message', function(event) {
var cmd = JSON.parse(event.data);
if (cmd.method === 'ssCurrentTime') {
    console.log('Current time is ' + cmd.arg + ' seconds.');
}
});

// Request current time.
var ssiframe = document.getElementById('ssembed').contentWindow;
ssiframe.postMessage('{"method": "getCurrentTime"}', 'https://www.soundslice.com');

Current time

Post a getCurrentTime message, then listen for ssCurrentTime. The arg will be in seconds as a float.

Note: If your slice has multiple recordings, duration and current time will likely change as the user switches recordings. When a user switches recordings, the Soundslice player maintains the current bar and percentage into that bar, seeking the underlying audio appropriately.

Total time (duration)

Post a getDuration message, then listen for ssDuration. The arg will be in seconds as a float.

See the note about multiple recordings in the “Current time” section above.

Current bar number

Post a getCurrentBar message, then listen for ssCurrentBar. The arg will be the current bar (measure) number, zero-based, as an integer.

Note: For slices that have any sort of repeat — repeat bars, “Da Segno,” “D.C. al Fine,” etc. — this number represents the “unrolled” bar number, which does not necessarily correspond to the displayed bar number that musicians refer to when reading the slice. For example, in a slice comprised of five bars, with a repeat bar at the end, the unrolled bar count would be 10 and the current bar number for the first bar would be 0 at start and 5 the second time through.

Total bar count

Post a getBarCount message, then listen for ssBarCount. The arg will be the total number of bars (measures), as an integer.

See the note about bar repeats in the “Current bar number” section above.

Notation dimensions (width/height)

Post a getNotationDimensions message, then listen for ssNotationDimensions. The arg will be an object with width and height keys. Both values are numbers.

These width and height values are the total width and height of the notation, in pixels. This is not necessarily the same as the currently visible width and height. If the player’s current zoom level and layout result in offscreen notation (showing horizontal or vertical scrollbars), then the ssNotationDimensions values will be larger than the player’s width and height.

To get the width and height of the player itself, simply look at the dimensions of your <iframe>. No need to call our API for that.

Current speed

Post a getSpeed message, then listen for ssSpeed. The arg will be the current speed, as a float, where 1 is normal speed and 0.5 is half speed.

Current volume

Post a getVolume message, then listen for ssVolume. The arg will be the current volume, as a float, where 1 is full volume and 0 is muted.

Notation visibility

Post a getNotationVisibility message, then listen for ssNotationVisibility. The arg will be a boolean: true if notation is currently visible and false if notation has been hidden.

Fullscreen availability

Post a getFullscreenSupport message, then listen for ssFullscreenSupport. The arg will be a boolean: true if the current browser/device supports web pages going fullscreen and false otherwise. Note that iOS Safari does not support web pages going fullscreen.