Skip to main content

Speakup Article Player - JavaScript API

This documentation describes how to use the Speakup Article Player JavaScript API to receive events and control the player state.


Initialization​

The API is automatically available through the global $sp object as soon as the player is loaded and initialized.

// Check if the API is ready
if ($sp?.isReady()) {
// The API is ready to use
console.log('Speakup Article Player API is ready!');
}
Availability

The $sp object is created immediately when the player loads, but isReady() returns true only after full initialization is complete.


Available Events​

play Event​

Emitted when the player starts playing a track.

$sp.onPlay((eventData) => {
console.log('Player started playing:', eventData);
});

pause Event​

Emitted when the player is paused.

$sp.onPause((eventData) => {
console.log('Player paused:', eventData);
});

statechange Event​

Emitted when the player play/pause state changes.

Note

This event is not emitted when the current song changes. Use the songchange event for track changes.

$sp.onStateChange((eventData) => {
console.log('Player state changed (play/pause):', eventData);
});

songchange Event​

Emitted when the current track changes.

$sp.onSongChange((eventData) => {
console.log('Song changed:', eventData);
});

destroy Event​

Emitted just before the player is destroyed. This allows listeners to perform their own cleanup operations.

$sp.onDestroy((eventData) => {
console.log('Player is being destroyed:', eventData);
// Perform your cleanup here
});

Event Data Structure​

All events (play, pause, songchange) share the same data structure:

{
type: 'play', // Event type ('play', 'pause', 'songchange')
timestamp: 1640995200000, // Unix timestamp
currentState: { // Current player state
isPlaying: true,
currentSong: { ... },
timestamp: 1640995200000
},
song: { // Current track data
id: 'podcast-123',
title: 'Podcast Title',
articleUrl: 'https://example.com/article',
author: 'Author Name'
}
}

State Change Event Data​

The statechange event contains both current and previous state:

{
type: 'statechange',
timestamp: 1640995200000,
currentState: {
isPlaying: true,
currentSong: { ... },
timestamp: 1640995200000
},
previousState: {
isPlaying: false,
currentSong: { ... },
timestamp: 1640995100000
}
}

Destroy Event Data​

The destroy event contains the final state:

{
type: 'destroy',
timestamp: 1640995200000,
currentState: {
isPlaying: false,
currentSong: { ... },
timestamp: 1640995200000
}
}

API Methods​

Event Listener Methods​

MethodDescriptionReturns
onPlay(callback)Registers a listener for the play eventFunction to remove the listener
onPause(callback)Registers a listener for the pause eventFunction to remove the listener
onStateChange(callback)Registers a listener for state changesFunction to remove the listener
onSongChange(callback)Registers a listener for track changesFunction to remove the listener
onDestroy(callback)Registers a listener for the destroy eventFunction to remove the listener
on(eventType, callback)Generic method to register listenersFunction to remove the listener
off(eventType, callback)Removes a specific listener-
removeAllListeners(eventType?)Removes all listeners for an event type-

State Methods​

MethodDescriptionReturns
getState()Returns the current player state{ isPlaying, currentSong, timestamp }
isPlaying()Checks if the player is playingboolean
getCurrentSong()Returns info about the current trackSong object or null
isReady()Checks if the API is readyboolean
destroy()Destroys the player and cleans up-

Method Details​

onPlay(callback)​

Registers a listener for the play event.

Parameters:

  • callback (function): Function called when the player starts playing

Returns: Function to remove the listener

const removeListener = $sp.onPlay((data) => {
console.log('Playing:', data.song.title);
});

// To remove the listener later
removeListener();

onPause(callback)​

Registers a listener for the pause event.

Parameters:

  • callback (function): Function called when the player is paused

Returns: Function to remove the listener

onStateChange(callback)​

Registers a listener for state changes.

Parameters:

  • callback (function): Function called when the player state changes

Returns: Function to remove the listener

onSongChange(callback)​

Registers a listener for track changes.

Parameters:

  • callback (function): Function called when the track changes

Returns: Function to remove the listener

$sp.onSongChange((data) => {
console.log('Now playing:', data.song.title);
console.log('Author:', data.song.author);
});

onDestroy(callback)​

Registers a listener for the destroy event. This event is emitted just before the player is destroyed.

Parameters:

  • callback (function): Function called when the player is about to be destroyed

Returns: Function to remove the listener

$sp.onDestroy((data) => {
console.log('Player is being destroyed');
// Clean up your custom resources here
});

on(eventType, callback)​

Generic method to register listeners.

Parameters:

  • eventType (string): Event type ('play', 'pause', 'statechange', 'songchange', 'destroy')
  • callback (function): Callback function

Returns: Function to remove the listener

$sp.on('play', (data) => {
console.log('Playing via generic method');
});

off(eventType, callback)​

Removes a specific listener.

Parameters:

  • eventType (string): Event type
  • callback (function): Callback function to remove

removeAllListeners(eventType)​

Removes all listeners for an event type.

Parameters:

  • eventType (string, optional): Event type. If not specified, removes all listeners

getState()​

Returns the current player state.

Returns: Object with the current state

const state = $sp.getState();
console.log('Current state:', state);
// { isPlaying: true, currentSong: {...}, timestamp: 1640995200000 }

isPlaying()​

Checks if the player is playing.

Returns: boolean - true if the player is playing

if ($sp.isPlaying()) {
console.log('Player is currently playing');
}

getCurrentSong()​

Returns information about the current track.

Returns: Object with the current track data or null if not available

const currentSong = $sp.getCurrentSong();
if (currentSong) {
console.log('Current song:', currentSong.title);
}

isReady()​

Checks if the API is initialized and ready to use.

Returns: boolean - true if the API is ready

destroy()​

Destroys the Speakup Article Player and cleans up all resources.

This method will:

  • Stop player playback
  • Reset the DOM to its original state
  • Remove all event listeners and timers
  • Clean up internal state
Important

After calling destroy(), if another player is set up on the page, all event listeners will need to be re-instantiated.

// Listen for destroy event before destroying
$sp.onDestroy((data) => {
console.log('Cleaning up my custom resources...');
});

// Destroy the player
$sp.destroy();

// After destroy, isReady() returns false
console.log($sp.isReady()); // false

Usage Examples​

Basic Example​

// Wait for the API to be ready
function waitForSpeakUpAPI() {
return new Promise((resolve) => {
const checkAPI = () => {
if ($sp?.isReady()) {
resolve();
} else {
setTimeout(checkAPI, 100);
}
};
checkAPI();
});
}

// Use the API
waitForSpeakUpAPI().then(() => {
// Register event listeners
$sp.onPlay((data) => {
console.log('🎡 Started playing:', data.song.title);
});

$sp.onPause((data) => {
console.log('⏸️ Paused:', data.song.title);
});

$sp.onSongChange((data) => {
console.log('πŸ”„ Song changed to:', data.song.title);
});

// Check the current state
console.log('Current state:', $sp.getState());
});

Example with Error Handling​

if ($sp?.isReady()) {
try {
const removePlayListener = $sp.onPlay((data) => {
// Handle the play event
updateUI(data);
});

// Remove the listener after 30 seconds
setTimeout(() => {
removePlayListener();
console.log('Play listener removed');
}, 30000);
} catch (error) {
console.error('Error setting up Speakup Article Player listeners:', error);
}
} else {
console.warn('Speakup Article Player API not available');
}

Important Notes​

  1. Initialization: The API is available as soon as the PlayerContext component mounts. The $sp object is created immediately, but isReady() returns true only after full initialization.

  2. Error Handling: Always check if the API is available before using it.

  3. Performance: Listeners are called asynchronously and do not block the user interface.

  4. Cleanup: Remember to remove listeners when they are no longer needed to avoid memory leaks.

  5. Browser Support: The API is compatible with all modern browsers that support ES6+.


Troubleshooting​

API is not available​

if (!$sp) {
console.error('Speakup Article Player not loaded');
} else if (!$sp.isReady()) {
console.warn('Speakup Article Player not ready yet');
}

Listeners are not being called​

  1. Verify that the API is initialized with isReady()
  2. Check that the player is actually loaded on the page
  3. Verify that there are no JavaScript errors in the console

Performance issues​

  • Remove unused listeners
  • Avoid heavy operations in event callbacks
  • Use removeAllListeners() to clean up all listeners if necessary

Need Help?​

If you have any doubts, do not hesitate to contact us at support@audioboost.com.