Listening Access

1. APIs

1.1. createListener

Description

This API is used to create a listening instance.

Example

  • listenPlayer = playerjs.createListener(options: Object)

Parameter

Field Type Description Default Value Required
devId string Unique identifier of a device Yes
channel string Channel number Yes
mediaProtocol string Media protocol: WS WS No
expireTime Number The address expiration time (in seconds) 20 No
optId String Operation ID, used for operation logging purposes "" No
addrType number Address type: 1 for WAN, 2 for LAN 1 No
startPrompt ArrayBuffer Start listening prompt sound. PCM format, 8000Hz sample rate, 16-bit, mono channel. No
stopPrompt ArrayBuffer Stop listening prompt sound. PCM format, 8000Hz sample rate, 16-bit, mono channel. No

1.2. Methods

1.2.1. start

Description

Start listening.

Example

  • listenPlayer.start()

1.2.2. stop

Description

Stop listening.

Example

  • listenPlayer.stop()

1.2.3. getStartTime

Description

This method is used to obtain the start time of the listening session, returning the UTC timestamp to the client in milliseconds.

Example

  • listenPlayer.getStartTime()

Note: To get the end time, please refer to the afterDestroy hook in HOOKS.

1.2.4. getDuration

Description

This method is used to obtain the duration of the listening session (in milliseconds).

Example

  • listenPlayer.getDuration()

1.3. Properties

1.3.1. volume: number

Description

This property is used to set the play volume on the client side.

Example

  • listenPlayer.volume = 0.5

This property is used to set the value range as a number, representing the expected channel volume with a range from 0 to 1.

2. Interface Examples

<script src="./h5-player.js"></script>
<script>
 const H5Player = playerjs;
 H5Player.config = {
     baseURL: 'http://192.168.132.72:21250'
 };
 H5Player.defaultHeaders = {
     _appId: '1',
     _tenantId: '2',
 }
 let listenPlayer = null;
 // Open listening
 function openListener() {
     if (listenPlayer) {
         alert('Please close the current listener first.');
         return;
     };
     listenPlayer = H5Player.createListener({
         devId: '03d430b6365943f1b02bdde73aceddf8',
         channel: 1
     });
     listenPlayer.hooks.afterLoaded.tap('play', () => {
         listenPlayer.start();
     });
     listenPlayer.hooks.onError.tap('auto close', () => {
         console.log('auto close');
     });
 }
 // Close listening
 function closeListener() {
     if (listenPlayer) {
         listenPlayer.stop();
         listenPlayer = null;
     }
 }
</script>