The LinkVisual application-side software development kit (SDK) provides features such as audio and video playback and voice intercom.
| Dependent SDKs | Overview |
| API Channel | Provides API channel capabilities. |
Initialization
Before you initialize the LinkVisual Video Media SDK, you must integrate the security image. For more information, see Integrate a security image.
Import dependencies
// 1. Add a reference to the Aliyun Maven repository in the build.gradle file of the root directory.
allprojects {
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
}
}
// 2. Add dependencies to the app's build.gradle file.
implementation('com.aliyun.iotx:linkvisual-media:1.2.21-ilop') For more information about version releases, see LinkVisual SDK update history.
Obfuscation configuration
# Keep and do not warn about LinkVisual.
-dontwarn com.aliyun.iotx.linkvisual.**
-dontwarn com.google.android.exoplayer2.**
-keep class com.aliyun.iotx.linkvisual.media.** { *; }Usage
There are three types of video players, each with different features.
- Live player
- Used for RTMP live sources and provides low latency.
- Supports P2P. This feature requires a camera that has the LinkVisual device-side SDK integrated.
- Device recording video-on-demand (VOD) player
Used to play back recordings from a device. You can adjust the playback progress.
- HLS player
- Used for playing back HLS-based cloud recordings. It supports MPEG-TS and fMP4 containers and AES-128 encryption.
- Two types of HLS players are available: a self-developed HLS player and a player encapsulated based on ExoPlayer.
| Feature | Live player | Device recording VOD player | HLS player (self-developed) | HLS player (ExoPlayer) |
| Video playback | ✓ | ✓ | ✓ | ✓ |
| Audio playback | ✓ | ✓ | ✓ | ✓ |
| Pause/Resume | x | ✓ | ✓ | ✓ |
| Seek to a specific position | x | ✓ | ✓ | ✓ |
| Total duration | x | ✓ | ✓ | ✓ |
| Current playback progress | x | ✓ | ✓ | ✓ |
| Player state change notifications | ✓ | ✓ | ✓ | ✓ |
| Set playback volume | ✓ | ✓ | ✓ | ✓ |
| Variable speed playback | x | ✓ | ✓ | ✓ |
| Loop playback | x | x | x | ✓ |
| Frame-by-frame playback | x | ✓ | ✓ | x |
| Set image scaling mode | ✓ | ✓ | ✓ | ✓ |
| Player screenshot | ✓ | ✓ | ✓ | ✓ |
| Save screenshot to file | ✓ | ✓ | ✓ | x |
| Record while playing | ✓ | ✓ | ✓ | x |
| Hardware decoding | ✓ | ✓ | ✓ | ✓ |
| Digital zoom | ✓ | ✓ | ✓ | ✓ |
| Provide YUV data | ✓ | ✓ | ✓ | x |
| Provide SEI data | ✓ | ✓ | ✓ | x |
The following code samples show how to use the players:
- Live player
// Construct a player instance. LivePlayer player = new LivePlayer(getApplicationContext()); // Set the TextureView. player.setTextureView(textureView); // Set the required state listener. player.setOnPlayerStateChangedListener(new OnPlayerStateChangedListener() { @Override public void onPlayerStateChange(int playerState) { Log.d(TAG, "play state= " + playerState); switch (playerState) { case Player.STATE_BUFFERING: break; case Player.STATE_IDLE: break; case Player.STATE_READY: break; case Player.STATE_ENDED: break; default: break; } } }); // Set the error listener. player.setOnErrorListener(new OnErrorListener() { @Override public void onError(PlayerException exception) { makeToast("errorcode: " + exception.getCode() + "\n" + exception.getMessage()); } }); // Set the RTMP address. player.setDataSource("rtmp://xx.xx.xx.xx/live"); // Set the data source preparation listener. player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared() { // Start playback after the data source is ready. player.start(); } }); player.prepare(); ... // Stop playback. player.stop(); ... // Release player resources. player.release(); - Device recording VOD player
// Construct a player instance. VodPlayer player = new VodPlayer(getApplicationContext()); // Set the TextureView. player.setTextureView(textureView); // Set the required state listener. player.setOnPlayerStateChangedListener(new OnPlayerStateChangedListener() { @Override public void onPlayerStateChange(int playerState) { Log.d(TAG, "play state= " + playerState); switch (playerState) { case Player.STATE_BUFFERING: break; case Player.STATE_IDLE: break; case Player.STATE_READY: break; case Player.STATE_ENDED: break; default: break; } } }); // Set the error listener. player.setOnErrorListener(new OnErrorListener() { @Override public void onError(PlayerException exception) { makeToast("errorcode: " + exception.getCode() + "\n" + exception.getMessage()); } }); // Set the RTMP address that supports VOD. player.setDataSource("rtmp://xxxx"); // Set the data source preparation listener. player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared() { // Start playback after the data source is ready. player.start(); } }); player.prepare(); ... // Pause playback. player.pause(); ... // Resume playback. player.start(); ... // Stop playback. player.stop(); ... // Release player resources. player.release(); - HLS cloud recording player (self-developed)
HlsPlayer player = new HlsPlayer(getApplicationContext()); // Set the TextureView. player.setTextureView(textureView); // Set the error listener. player.setOnErrorListener(new OnErrorListener() { @Override public void onError(PlayerException exception) { makeToast("errorcode: " + exception.getCode() + "\n" + exception.getMessage()); } }); // Set the state listener. player.setOnPlayerStateChangedListener(new OnPlayerStateChangedListener() { @Override public void onPlayerStateChange(int playerState) { switch (playerState) { case Player.STATE_BUFFERING: break; case Player.STATE_IDLE: break; case Player.STATE_READY: break; case Player.STATE_ENDED: break; default: break; } } }); // Set the M3U8 address. player.setDataSource("http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear3/prog_index.m3u8"); // Set the data source preparation listener. player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared() { // Start playback after the data source is ready. player.start(); } }); player.prepare(); ... // Pause playback. player.pause(); ... // Resume playback. player.start(); ... // Stop playback. player.stop(); ... // Release player resources. player.release(); ... HLS cloud recording player (ExoPlayer)We recommend that you use the self-developed player.ExoHlsPlayer player = new ExoHlsPlayer(getApplicationContext()); // Set the TextureView. player.setTextureView(textureView); // You can also use ExoPlayer's SimpleExoPlayerView as the UI component for the player. // simpleExoPlayerView.setPlayer(player.getExoPlayer()); // simpleExoPlayerView.requestFocus(); // Set the error listener. player.setOnErrorListener(new OnErrorListener() { @Override public void onError(PlayerException exception) { makeToast("errorcode: " + exception.getCode() + "\n" + exception.getMessage()); } }); // Set the state listener. player.setOnPlayerStateChangedListener(new OnPlayerStateChangedListener() { @Override public void onPlayerStateChange(int playerState) { switch (playerState) { case Player.STATE_BUFFERING: break; case Player.STATE_IDLE: break; case Player.STATE_READY: break; case Player.STATE_ENDED: break; default: break; } } }); // Set the M3U8 address. player.setDataSource("http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear3/prog_index.m3u8"); // Set the data source preparation listener. player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared() { // Start playback after the data source is ready. player.start(); } }); player.prepare(); ... // Pause playback. player.pause(); ... // Resume playback. player.start(); ... // Stop playback. player.stop(); ... // Release player resources. player.release(); ... // Take a screenshot. textureview.getBitmap();
Player states
You can set a player state listener to receive state change events. You can use these events to update the related UI elements.
State change events are triggered by playback errors or when playback is manually stopped.

- IDLE: The player is not playing any content.
- BUFFERING: The player is buffering and cannot start playback from the current position. This state occurs when buffering at the start of playback or rebuffering after a seek operation.
- READY: The player is playing content. This state occurs when the first frame is rendered or when playback of new content starts after seek buffering is complete. For a VOD player, if you seek or the playback reaches the end of the file, the
OnCompletionListener.onCompletion()method is called. The state does not switch to ENDED. - ENDED: Playback has ended. The player switches to this state after a playback error occurs or after `stop()` is called.
API reference
- LinkVisualMedia Note LinkVisualMedia is used to pre-establish connections. You can ignore this if you do not need to pre-establish connections to optimize the time to first frame for live streaming.
- Obtain a singleton instance
LinkVisualMedia getInstance(); - Initialization method
/** * The global resource initialization method. The SDK is automatically initialized. You do not need to call this method. * @Deprecated */ void init(); - Request a pre-established connection for an IPC device
/** * Call this method before playback to try to pre-establish a connection with an IPC device. A successful pre-connection speeds up the time to first frame for live streaming. * @param iotId The iotId of the online IPC device. */ void preConnectByIotId(String iotId);
- Obtain a singleton instance
- LivePlayer
- Constructor
/** * Constructor. * @param applicationContext ApplicationContext. */ LivePlayer(Context applicationContext); - Set an unencrypted playback source
/** * Sets the playback source. * @param url The RTMP address. */ void setDataSource(String url) throws IllegalArgumentException; - Set an encrypted playback source
/** * Sets an encrypted playback source. This method is applicable only to RTMP addresses provided by the LinkVisual cloud. * @param url The RTMP source address. * @param isEncrypted Specifies whether the source is encrypted. * @param decryptIv The decryption vector. A 16-byte array. * @param decryptKey The decryption key. A 16-byte array. * @throws IllegalArgumentException */ void setDataSource(String url, boolean isEncrypted, byte[] decryptIv, byte[] decryptKey) throws IllegalArgumentException; - Set the data source for IPC live streaming. Encryption and forced I-frames are enabled for this API.
/** * Sets the data source for IPC live streaming. Encryption and forced I-frames are enabled for this API. * @param iotId The iotId of the IPC device. * @param streamType The stream type. If multiple streams are available, specify this parameter. C.STREAM_TYPE_MAJOR indicates the major stream. C.STREAM_TYPE_MINOR indicates the minor stream. */ void setIPCLiveDataSource(String iotId, int streamType); - Set the data source for IPC live streaming. Encryption and forced I-frames are enabled for this API.
/** * Sets the data source for IPC live streaming. Encryption and forced I-frames are enabled for this API. * @param iotId The iotId of the IPC device. * @param streamType The stream type. If multiple streams are available, specify this parameter. C.STREAM_TYPE_MAJOR indicates the major stream. C.STREAM_TYPE_MINOR indicates the minor stream. * @param cacheDurationInMs The duration of the video cached in the cloud. If data exists, the data is fast-forwarded. Unit: ms. We recommend that you do not set this value to a value greater than the duration of one group of pictures (GOP). */ void setIPCLiveDataSource(String iotId, int streamType, int cacheDurationInMs); - Set the data source for IPC live streaming
/** * Sets the data source for IPC live streaming. * * @param iotId The iotId of the device. * @param streamType The stream type. If multiple streams are available, specify this parameter. C.STREAM_TYPE_MAJOR indicates the major stream. C.STREAM_TYPE_MINOR indicates the minor stream. * @param relayEncrypted Specifies whether to encrypt the cloud relay stream. We recommend that you enable this feature. * @param relayEncryptType The encryption type of the cloud relay stream. * @param forceIFrame Specifies whether to force I-frames. We recommend that you enable this feature. * @param cacheDurationInMs The duration of the video cached in the cloud. If data exists, the data is fast-forwarded. Unit: ms. We recommend that you do not set this value to a value greater than the duration of one GOP. */ void setIPCLiveDataSource(String iotId, int streamType, boolean relayEncrypted, int relayEncryptType, boolean forceIFrame, int cacheDurationInMs) - Prepare the data source
/** * Checks and prepares data. */ void prepare(); - Start video playback
/** * Starts video playback. */ void start(); - Stop playback
/** * Stops playback. */ void stop(); - Reset the player
/** * Resets the player. */ void reset(); - Release player resources
/** * Releases player resources. */ void release(); - Set the number of reconnection attempts
/** * Sets the number of reconnection attempts. * Reconnection is attempted only when a {@link PlayerException#SUB_CODE_UNEXPECTED_PULL_STREAM_ERROR} error occurs. * @param count The default value is 0. We recommend that you set the value to 3 or less. */ void setReconnectCount(int count); - Take a screenshot
/** * Takes a screenshot of the current video frame. * @return Returns null if no frame is displayed. */ Bitmap snapShot(); - Save a screenshot to a file
/** * Takes a screenshot of the current video frame. * @param jpegFile The file to which the JPEG screenshot is saved. * @return true indicates that the file is saved. */ boolean snapShotToFile(File jpegFile); - Start recording
/** * Starts to record the current playback content and saves it to a specified file in MPEG-4 format. * The filename extension must be .mp4. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @param contentFile * @return Indicates whether the operation is successful. */ boolean startRecordingContent(File contentFile) throws IOException; - Obtain the duration of the current recording
/** * Gets the duration of the current recording. * @return Unit: ms. */ long getCurrentRecordingContentDuration(); - Stop recording
/** * Stops recording the playback content. * @return Indicates whether the operation is successful. */ boolean stopRecordingContent(); - Set the player volume
/** * Sets the player volume. * @param audioVolume The value ranges from 0 to 1. A value of 0 indicates silence. */ void setVolume(float audioVolume); - Set the audio stream channel type
/** * Sets the audio stream channel type. See {@link android.media.AudioManager}. The default value is AudioManager.STREAM_MUSIC. * If audio is playing, a brief pause may occur because the AudioTrack is recreated. * @param audioStreamType */ void setAudioStreamType(int audioStreamType); - Set the image scaling mode
/** * Sets the video image scaling mode. The default value is {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING}. * @param videoScalingMode See: * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT} * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} */ void setVideoScalingMode(int videoScalingMode); - Set the decoder strategy
/** * Sets the decoder strategy. This setting takes effect globally. * By default, forced software decoding is used. * * @param decoderStrategy * HARDWARE_FIRST - Hardware decoding is prioritized. * FORCE_SOFTWARE - Forced software decoding. */ void setDecoderStrategy(DecoderStrategy decoderStrategy) - Obtain the decoder type for the current stream
/** * Gets the decoder type for the current stream, which can be software or hardware decoding. * * @return Returns null if no stream is being played. * HARDWARE - Hardware decoding. * SOFTWARE - Software decoding. */ DecoderType getDecoderType() - Set the rendering policy for when playback stops
/** * Sets the drawing policy for when playback stops. * @param playerStoppedDrawingMode * ALWAYS_KEEP_LAST_FRAME Always keeps the last frame when playback stops. * KEEP_LAST_FRAME_WITHOUT_ERROR Keeps the last frame only if no error occurs when playback stops. This is the default mode. * ALWAYS_BLACK Always displays a black screen when playback stops. */ void setPlayerStoppedDrawingMode(PlayerStoppedDrawingMode playerStoppedDrawingMode) - Set a fixed number of cached frames for the player
/** * Sets a fixed number of cached frames for the player. * @param frameCount The fixed number of cached frames for the player. The value ranges from 0 to 50. A larger value indicates higher latency and better playback smoothness. The default value is 5. */ void setBufferedFrameCount(int frameCount); - Set the maximum anti-jitter buffer duration for the player
/** * Sets the maximum anti-jitter buffer duration for the player. The default value is 1,000 ms. The value ranges from 200 ms to 3,000 ms. * @param jitterBufferSizeInMs */ void setMaxJitterBufferSizeInMs(int jitterBufferSizeInMs); - Set the SurfaceView
/** * Sets the SurfaceView. It must be a GLSurfaceView. Only one player is allowed to play in the same window at a time. To support multiple player instances, use TextureView. * Note: For a GLSurfaceView, you must call the onResume and onPause methods of the GLSurfaceView in the onResume and onPause callback methods of the activity. * Do not set a background color for the GLSurfaceView and its container. * @param surfaceview */ void setSurfaceView(SurfaceView surfaceview); - Clear the SurfaceView
/** * Clears the SurfaceView. */ void clearSurfaceView(); - Set the TextureView
/** * Sets the TextureView. The same TextureView cannot be shared by multiple players. ZoomableTextureView with gesture-based scaling is provided for reference. * @param textureview */ void setTextureView(TextureView textureView); - Clear the TextureView
/** * Clears the TextureView. */ void clearTextureView(); - Specify whether to use an external renderer for YUV data Note This is an advanced mode. If you enable this mode, the player no longer renders images internally. You must set a listener for YUV data changes and promptly retrieve the cached YUV data frames and render them immediately using the YUV data retrieval API.
/** * Specifies whether to use an external renderer for YUV data. * @param useExternalRender true indicates that an external renderer is used. false indicates that the internal renderer is used. */ void setUseExternalRender(boolean useExternalRender); - Check whether an external renderer is used
/** * Checks whether an external renderer is used. * @return true indicates that an external renderer is used. false indicates that the internal renderer is used. */ boolean useExternalRender(); - Set the video quality and network listener
/** * Sets the video quality and network listener. * @param listener */ void setOnVideoQualityListener(OnVideoQualityListener listener); public interface OnVideoQualityListener { /** * This method is called back when there is no data in the buffer for a period of time. This is usually caused by an unstable network. You can consider prompting the user to reduce the definition. */ void onVideoJitterBufferEmpty(); } - Set the external renderer listener
/** * Sets the external renderer listener. * @param onExternalRenderListener */ void setOnExternalRenderListener(OnExternalRenderListener onExternalRenderListener);public interface OnExternalRenderListener { public interface OnExternalRenderListener { /** * Notifies that a YUV data frame needs to be rendered. * @param width The width of the video frame. * @param height The height of the video frame. * @param timestamp The timestamp. Unit: ms. */ void onVideoFrameUpdate(int width, int height ,long timestamp); } - Retrieve YUV frame data for rendering
/** * Gets YUV frame data for rendering. This method is valid only in external rendering mode. * @return A video frame in YUV420p format. */ Yuv420pFrame getYuvFrame(); - Set the SEI information listener
/** * Sets the SEI information listener. * * @param seiInfoBuffer Creates a cache for storing SEI frame data. Make sure that the length of the SEI information in the stream does not exceed the cache size. Frames that exceed the size are discarded. * @param onSeiInfoListener */ void setOnSeiInfoListener(SeiInfoBuffer seiInfoBuffer, OnSeiInfoListener onSeiInfoListener); interface OnSeiInfoListener { /** * This method is called back when the SEI information is updated. * Do not perform blocking operations. Process the seiInfoBuffer data promptly in this callback. * @param seiInfoBuffer */ void onSeiInfoUpdate(SeiInfoBuffer seiInfoBuffer); } - Set the data source preparation event listener
/** * Sets the data source preparation event listener. * @param listener */ void setOnPreparedListener(OnPreparedListener listener); public interface OnPreparedListener { /** * This method is called back when the data source is ready. */ void onPrepared(); } - Set the player error event listener
/** * Sets the player error event listener. For more information about error types, see: * {@link PlayerException.SOURCE_ERROR} * {@link PlayerException.RENDER_ERROR} * {@link PlayerException.UNEXPECTED_ERROR} * @param listener */ void setOnErrorListener(OnErrorListener listener); public interface OnErrorListener { /** * This method is called back when a player error occurs. See * {@link PlayerException} * @param exception */ void onError(PlayerException exception); } - Set the player state change event listener
/** * Sets the player state change event listener. See the state diagram. * @param listener */ void setOnPlayerStateChangedListener(OnPlayerStateChangedListener listener); public interface OnPlayerStateChangedListener { /** * This method is called back when the player state changes. * * @param playerState See * {@link PlayerState#STATE_IDLE} * {@link PlayerState#STATE_BUFFERING} * {@link PlayerState#STATE_READY} * {@link PlayerState#STATE_ENDED} */ void onPlayerStateChange(int playerState); } - Set the first frame rendering event listener
/** * Sets the first frame rendering event listener. * @param listener */ void setOnRenderedFirstFrameListener(OnRenderedFirstFrameListener listener); public interface OnRenderedFirstFrameListener { /** * This method is called back when the first frame is rendered. */ void onRenderedFirstFrame(); } - Set the video size change callback
/** * Sets the video size change callback. * @param listener */ void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener); public interface OnVideoSizeChangedListener { /** * This method is called back when the content size changes. * * @param width The width of the video content. Unit: pixel. * @param height The height of the video content. Unit: pixel. */ void onVideoSizeChanged(int width, int height); } - Obtain the volume
/** * Gets the volume. * @return The value ranges from 0 to 1. A value of 0 indicates silence. */ float getVolume(); - Obtain the player state
/** * Gets the playback state. * @return The state enumeration: * {@link PlayerState#STATE_IDLE} The initial state of the player. * {@link PlayerState#STATE_BUFFERING} The buffering state. * {@link PlayerState#STATE_READY} The state where buffering is complete and playback starts. * {@link PlayerState#STATE_ENDED} The playback completion state. */ int getPlayState(); - Obtain the connection type of the current stream
/** * Gets the connection type of the current stream. * Call this method 5 seconds after the player enters the {@link PlayerState#STATE_READY} state to accurately reflect the current stream type. * * @return {@link StreamConnectType} */ StreamConnectType getStreamConnectType(); - Obtain information about the current stream, such as the frame rate and bitrate
/** * Gets information about the current stream, such as frame rate and bitrate. * This method is valid when the player is in the {@link PlayerState#STATE_BUFFERING} or {@link PlayerState#STATE_READY} state. * @return A JSON string that contains information such as frame rate and bitrate. */ PlayInfo getCurrentPlayInfo();
- Constructor
- VodPlayer
- Constructor
/** * Constructor. * @param applicationContext ApplicationContext. */ VodPlayer(Context applicationContext); - Set an unencrypted playback source
/** * Sets the playback source. * @param url The RTMP address. */ void setDataSource(String url) throws IllegalArgumentException; - Set an encrypted playback source
/** * Sets an encrypted playback source. This method is applicable only to RTMP addresses provided by the LinkVisual cloud. * @param url The RTMP source address. * @param isEncrypted Specifies whether the source is encrypted. * @param decryptIv The decryption vector. A 16-byte array. * @param decryptKey The decryption key. A 16-byte array. * @throws IllegalArgumentException */ void setDataSource(String url, boolean isEncrypted, byte[] decryptIv, byte[] decryptKey) throws IllegalArgumentException; - Set the data source to an IPC recording (by filename). Encryption is enabled for this API.
/** * Sets the data source to an IPC recording (by filename). Encryption is enabled for this API. * * @param iotId The iotId of the device. * @param fileName The name of the recording file. */ void setDataSourceByIPCRecordFileName(String iotId, String fileName); /** * Sets the data source to an IPC recording (by filename). Encryption is enabled for this API. * * @param iotId The iotId of the device. * @param fileName The name of the recording file. * @param seekToPositionInMs The initial offset relative to the file header. Unit: ms. */ void setDataSourceByIPCRecordFileName(String iotId, String fileName, long seekToPositionInMs); - Set the data source to an IPC recording (by time range)
/** * Sets the playback address to the local recording file of an IPC device that is connected to IoT Platform for a specified time range. * Encryption is enabled for this API. * see {@link #setDataSourceByIPCRecordTime(String, int, int, boolean, int, long)} * * @param iotId The iotId of the device. * @param beginTimeInS The start time of the recording. The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC. * @param endTimeInS The end time of the recording. The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC. * @param seekToPositionInMs The playback offset relative to beginTime. Unit: ms. */ void setDataSourceByIPCRecordTime(String iotId, int beginTimeInS, int endTimeInS, long seekToPositionInMs) /** * Sets the playback address to the local recording file of an IPC device that is connected to IoT Platform for a specified time range. * Encryption is enabled for this API. * see {@link #setDataSourceByIPCRecordTime(String, int, int, boolean, int, long)} * * @param iotId The iotId of the device. * @param beginTimeInS The start time of the recording. The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC. * @param endTimeInS The end time of the recording. The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC. * @param seekToPositionInMs The playback offset relative to beginTime. Unit: ms. * @param recordType The recording type. 0 indicates a scheduled recording. 1 indicates an alarm recording. */ void setDataSourceByIPCRecordTime(String iotId, int beginTimeInS, int endTimeInS, long seekToPositionInMs, int recordType) /** * Sets the playback address to the local recording file of an IPC device that is connected to IoT Platform for a specified time range. * * @param iotId The iotId of the device. * @param beginTimeInS The start time of the recording time range. UTC. The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC. * @param endTimeInS The end time of the recording time range. UTC. The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC. * @param encrypted Specifies whether the stream needs to be encrypted. We strongly recommend that you enable this feature. For products intended for overseas markets, make sure this feature is enabled. * @param encryptType The encryption type. Currently, only C.ENCRYPTE_AES_128 (AES-128) is supported. * @param seekToPositionInMs The playback offset relative to beginTimeInS. Unit: ms. * @param recordType The recording type. 0 indicates a scheduled recording. 1 indicates an alarm recording. * For security reasons, enable encryption unless you have specific requirements. */ void setDataSourceByIPCRecordTime(String iotId, int beginTimeInS, int endTimeInS, boolean encrypted, int encryptType, long seekToPositionInMs, int recordType) - Prepare the data source
/** * Checks and prepares data. */ void prepare(); - Start or resume video playback
/** * Starts or resumes video playback. */ void start(); - Pause playback
/** * Pauses playback. Call start() to resume playback. */ void pause(); - Seek to a specific position
/** * Seeks to a specific position. * @param positionInMs Milliseconds. */ void seekTo(long positionInMs); - Play frame by frame
/** * Plays frame by frame. To resume normal playback, call start(). * @return Indicates whether the call is successful. */ boolean playFrameByFrame(); - Stop playback
/** * Stops playback. */ void stop(); - Reset the player
/** * Resets the player. */ void reset(); - Release player resources
/** * Releases player resources. */ void release(); - Take a screenshot
/** * Takes a screenshot of the current video frame. * @return Returns null if no frame is displayed. */ Bitmap snapShot(); - Save a screenshot to a file
/** * Takes a screenshot of the current video frame. * @param jpegFile The file to which the JPEG screenshot is saved. * @return true indicates that the file is saved. */ boolean snapShotToFile(File jpegFile); - Start recording
/** * Starts to record the current playback content and saves it to a specified file in MPEG-4 format. * The filename extension must be .mp4. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @param contentFile * @return Indicates whether the operation is successful. */ boolean startRecordingContent(File contentFile) throws IOException; - Obtain the duration of the current recording
/** * Gets the duration of the current recording. * @return Unit: ms. */ long getCurrentRecordingContentDuration(); - Stop recording
/** * Stops recording the playback content. * @return Indicates whether the operation is successful. */ boolean stopRecordingContent(); - Set the player volume
/** * Sets the player volume. * @param audioVolume The value ranges from 0 to 1. A value of 0 indicates silence. */ void setVolume(float audioVolume); - Set the audio stream channel type
/** * Sets the audio stream channel type. See {@link android.media.AudioManager}. * If audio is playing, a brief pause may occur because the AudioTrack is recreated. * @param audioStreamType */ void setAudioStreamType(int audioStreamType); - Set the image scaling mode
/** * Sets the video image scaling mode. The default value is {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING}. * @param videoScalingMode See: * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT} * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} */ void setVideoScalingMode(int videoScalingMode); - Set the decoder strategy
/** * Sets the decoder strategy. This setting takes effect globally. * By default, forced software decoding is used. * * @param decoderStrategy * HARDWARE_FIRST - Hardware decoding is prioritized. * FORCE_SOFTWARE - Forced software decoding. */ void setDecoderStrategy(DecoderStrategy decoderStrategy) - Obtain the decoder type for the current stream
/** * Gets the decoder type for the current stream, which can be software or hardware decoding. * * @return Returns null if no stream is being played. * HARDWARE - Hardware decoding. * SOFTWARE - Software decoding. */ DecoderType getDecoderType() - Set the rendering policy for when playback stops
/** * Sets the drawing policy for when playback stops. * @param playerStoppedDrawingMode * ALWAYS_KEEP_LAST_FRAME Always keeps the last frame when playback stops. * KEEP_LAST_FRAME_WITHOUT_ERROR Keeps the last frame only if no error occurs when playback stops. This is the default mode. * ALWAYS_BLACK Always displays a black screen when playback stops. */ void setPlayerStoppedDrawingMode(PlayerStoppedDrawingMode playerStoppedDrawingMode) - Set the SurfaceView
/** * Sets the SurfaceView. It must be a GLSurfaceView. Only one player is allowed to play in the same window at a time. To support multiple player instances, use TextureView. * Note: For a GLSurfaceView, you must call the onResume and onPause methods of the GLSurfaceView in the onResume and onPause callback methods of the activity. * Do not set a background color for the GLSurfaceView and its container. * @param surfaceview */ void setSurfaceView(SurfaceView surfaceview); - Clear the SurfaceView
/** * Clears the SurfaceView. */ void clearSurfaceView(); - Set the TextureView
/** * Sets the TextureView. The same TextureView cannot be shared by multiple players. ZoomableTextureView with gesture-based scaling is provided for reference. * @param textureview */ void setTextureView(TextureView textureView); - Clear the TextureView
/** * Clears the TextureView. */ void clearTextureView(); - Specify whether to use an external renderer for YUV data
/** * Specifies whether to use an external renderer for YUV data. * @param useExternalRender true indicates that an external renderer is used. false indicates that the internal renderer is used. */ void setUseExternalRender(boolean useExternalRender); - Check whether an external renderer is used
/** * Checks whether an external renderer is used. * @return true indicates that an external renderer is used. false indicates that the internal renderer is used. */ boolean useExternalRender(); - Set the external renderer listener
/** * Sets the external renderer listener. * @param onExternalRenderListener */ void setOnExternalRenderListener(OnExternalRenderListener onExternalRenderListener);public interface OnExternalRenderListener { public interface OnExternalRenderListener { /** * Notifies that a YUV data frame needs to be rendered. * @param width The width of the video frame. * @param height The height of the video frame. * @param timestamp The timestamp. Unit: ms. */ void onVideoFrameUpdate(int width, int height ,long timestamp); } - Retrieve YUV frame data for rendering
/** * Gets YUV frame data for rendering. This method is valid only in external rendering mode. * @return A video frame in YUV420p format. */ Yuv420pFrame getYuvFrame(); - Set the SEI information listener
/** * Sets the SEI information listener. * * @param seiInfoBuffer Creates a cache for storing SEI frame data. Make sure that the length of the SEI information in the stream does not exceed the cache size. Frames that exceed the size are discarded. * @param onSeiInfoListener */ void setOnSeiInfoListener(SeiInfoBuffer seiInfoBuffer, OnSeiInfoListener onSeiInfoListener); interface OnSeiInfoListener { /** * This method is called back when the SEI information is updated. * Do not perform blocking operations. Process the seiInfoBuffer data promptly in this callback. * @param seiInfoBuffer */ void onSeiInfoUpdate(SeiInfoBuffer seiInfoBuffer); } - Set the data source preparation event listener
/** * Sets the data source preparation event listener. * @param listener */ void setOnPreparedListener(OnPreparedListener listener); interface OnPreparedListener { /** * This method is called back when the data source is ready. */ void onPrepared(); } - Set the player error event listener
/** * Sets the player error event listener. For more information about error types, see: * {@link PlayerException.SOURCE_ERROR} * {@link PlayerException.RENDER_ERROR} * {@link PlayerException.UNEXPECTED_ERROR} * @param listener */ void setOnErrorListener(OnErrorListener listener); interface OnErrorListener { /** * This method is called back when a player error occurs. See * {@link PlayerException} * @param exception */ void onError(PlayerException exception); } - Set the player state change event listener
/** * Sets the player state change event listener. * @param listener */ void setOnPlayerStateChangedListener(OnPlayerStateChangedListener listener); interface OnPlayerStateChangedListener { /** * This method is called back when the player state changes. * * @param playerState See * {@link PlayerState#STATE_IDLE} * {@link PlayerState#STATE_BUFFERING} * {@link PlayerState#STATE_READY} * {@link PlayerState#STATE_ENDED} */ void onPlayerStateChange(int playerState); } - Set the first frame rendering event listener
/** * Sets the first frame rendering event listener. * @param listener */ void setOnRenderedFirstFrameListener(OnRenderedFirstFrameListener listener); interface OnRenderedFirstFrameListener { /** * This method is called back when the first frame is rendered. */ void onRenderedFirstFrame(); } - Set the video size change callback
/** * Sets the video size change callback. * @param listener */ void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener); interface OnVideoSizeChangedListener { /** * This method is called back when the content size changes. * * @param width The width of the video content. Unit: pixel. * @param height The height of the video content. Unit: pixel. */ void onVideoSizeChanged(int width, int height); } - Set the playback completion event listener
/** * Sets the playback completion event listener. * After this event is received, you need to call stop() to switch the player state to STATE_END. * @param listener */ void setOnCompletionListener(OnCompletionListener listener); interface OnCompletionListener { /** * This method is called back when playback reaches the end of the file. */ void onCompletion(); } - Obtain the current playback position
/** * Gets the current playback position, which is the offset from the start position. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @return Unit: ms. */ long getCurrentPosition(); - Obtain the total video duration
/** * Gets the total video duration. * This method is valid only when the player is in the {@link #STATE_READY} state. * @return Unit: ms. */ long getDuration(); - Set the playback speed
/** * Sets the playback speed. The device must support the variable speed feature. * This method is valid only when the player is in the {@link PlayerState#STATE_BUFFERING} or {@link PlayerState#STATE_READY} state. * Sound is disabled by default when the speed is not 1x. * @param speed Only 1/16, 1/8, 1/4, 1/2, 1, 2, 4, 8, and 16 are supported. */ void setPlaybackSpeed(float speed) - Obtain the volume
/** * Gets the volume. * @return The value ranges from 0 to 1. A value of 0 indicates silence. */ float getVolume(); - Obtain the player state
/** * Gets the playback state. * @return The state enumeration: * {@link PlayerState#STATE_IDLE} The initial state of the player. * {@link PlayerState#STATE_BUFFERING} The buffering state. * {@link PlayerState#STATE_READY} The state where buffering is complete and playback starts. * {@link PlayerState#STATE_ENDED} The playback completion state. */ int getPlayState(); - Obtain the connection type of the current stream
/** * Gets the connection type of the current stream. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @return {@link StreamConnectType} */ StreamConnectType getStreamConnectType(); - Obtain information about the current stream, such as the frame rate and bitrate
/** * Gets information about the current stream, such as frame rate and bitrate. * This method is valid when the player is in the {@link PlayerState#STATE_BUFFERING} or {@link PlayerState#STATE_READY} state. * @return A JSON string that contains information such as frame rate and bitrate. */ PlayInfo getCurrentPlayInfo();
- Constructor
- HlsPlayer
- Constructor
/** * Constructor. * @param applicationContext ApplicationContext. */ HlsPlayer(Context applicationContext); - Set the playback address
/** * Sets the playback source. * @param url The HLS VOD address. */ void setDataSource(String url) throws IllegalArgumentException; - Set the playback address to the cloud recording file of a specified IPC device that is connected to IoT Platform
/** * Sets the playback address to the cloud recording file of a specified IPC device that is connected to IoT Platform. * * @param iotId The iotId of the device. * @param fileName The name of the recording file. */ void setDataSourceByIPCRecordFileName(String iotId, String fileName) - Set the playback address to the cloud recording file of a specified IPC device that is connected to IoT Platform
/** * Sets the playback address to the cloud recording file of a specified IPC device that is connected to IoT Platform. * * @param iotId The iotId of the device. * @param fileName The name of the recording file. * @param seekToPositionInMs The initial offset. Unit: ms. */ void setDataSourceByIPCRecordFileName(String iotId, String fileName, long seekToPositionInMs) - Prepare the data source
/** * Checks and prepares data. */ void prepare(); - Start or resume video playback
/** * Starts or resumes video playback. */ void start(); - Pause playback
/** * Pauses playback. Call start() to resume playback. */ void pause(); - Seek to a specific position
/** * Seeks to a specific position. * @param positionInMs Milliseconds. */ void seekTo(long positionInMs); - Play frame by frame
/** * Plays frame by frame. To resume normal playback, call start(). * @return Indicates whether the call is successful. */ boolean playFrameByFrame(); - Stop playback
/** * Stops playback. */ void stop(); - Reset the player
/** * Resets the player. */ void reset(); - Release player resources
/** * Releases player resources. */ void release(); - Take a screenshot
/** * Takes a screenshot of the current video frame. * @return Returns null if no frame is displayed. */ Bitmap snapShot(); - Save a screenshot to a file
/** * Takes a screenshot of the current video frame. * @param jpegFile The file to which the JPEG screenshot is saved. * @return true indicates that the file is saved. */ boolean snapShotToFile(File jpegFile); - Start recording
/** * Starts to record the current playback content and saves it to a specified file in MPEG-4 format. * The filename extension must be .mp4. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @param contentFile * @return Indicates whether the operation is successful. */ boolean startRecordingContent(File contentFile) throws IOException; - Obtain the duration of the current recording
/** * Gets the duration of the current recording. * @return Unit: ms. */ long getCurrentRecordingContentDuration(); - Stop recording
/** * Stops recording the playback content. * @return Indicates whether the operation is successful. */ boolean stopRecordingContent(); - Set the playback speed
/** * Sets the playback speed. * This method is valid only when the player is in the {@link PlayerState#STATE_BUFFERING} or {@link PlayerState#STATE_READY} state. * Sound is disabled by default when the speed is not 1x. * @param speed Only 1/16, 1/8, 1/4, 1/2, 1, 2, 4, 8, and 16 are supported. */ void setPlaybackSpeed(float speed) - Set the player volume
/** * Sets the player volume. * @param audioVolume The value ranges from 0 to 1. A value of 0 indicates silence. */ void setVolume(float audioVolume); - Set the audio stream channel type
/** * Sets the audio stream channel type. See {@link android.media.AudioManager}. * If audio is playing, a brief pause may occur because the AudioTrack is recreated. * @param audioStreamType */ void setAudioStreamType(int audioStreamType); - Set the image scaling mode
/** * Sets the video image scaling mode. The default value is {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING}. * @param videoScalingMode See: * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT} * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} */ void setVideoScalingMode(int videoScalingMode); - Set the decoder strategy
/** * Sets the decoder strategy. This setting takes effect globally. * By default, forced software decoding is used. * * @param decoderStrategy * HARDWARE_FIRST - Hardware decoding is prioritized. * FORCE_SOFTWARE - Forced software decoding. */ void setDecoderStrategy(DecoderStrategy decoderStrategy) - Obtain the decoder type for the current stream
/** * Gets the decoder type for the current stream, which can be software or hardware decoding. * * @return Returns null if no stream is being played. * HARDWARE - Hardware decoding. * SOFTWARE - Software decoding. */ DecoderType getDecoderType() - Set the rendering policy for when playback stops
/** * Sets the drawing policy for when playback stops. * @param playerStoppedDrawingMode * ALWAYS_KEEP_LAST_FRAME Always keeps the last frame when playback stops. * KEEP_LAST_FRAME_WITHOUT_ERROR Keeps the last frame only if no error occurs when playback stops. This is the default mode. * ALWAYS_BLACK Always displays a black screen when playback stops. */ void setPlayerStoppedDrawingMode(PlayerStoppedDrawingMode playerStoppedDrawingMode) - Set the SurfaceView
/** * Sets the SurfaceView. It must be a GLSurfaceView. Only one player is allowed to play in the same window at a time. To support multiple player instances, use TextureView. * Note: For a GLSurfaceView, you must call the onResume and onPause methods of the GLSurfaceView in the onResume and onPause callback methods of the activity. * Do not set a background color for the GLSurfaceView and its container. * @param surfaceview */ void setSurfaceView(SurfaceView surfaceview); - Clear the SurfaceView
/** * Clears the SurfaceView. */ void clearSurfaceView(); - Set the TextureView
/* * Sets the TextureView. The same TextureView cannot be shared by multiple players. ZoomableTextureView with gesture-based scaling is provided for reference. * @param textureview */ void setTextureView(TextureView textureView); - Clear the TextureView
/** * Clears the TextureView. */ void clearTextureView(); - Specify whether to use an external renderer for YUV data
/** * Specifies whether to use an external renderer for YUV data. * @param useExternalRender true indicates that an external renderer is used. false indicates that the internal renderer is used. */ void setUseExternalRender(boolean useExternalRender); - Check whether an external renderer is used
/** * Checks whether an external renderer is used. * @return true indicates that an external renderer is used. false indicates that the internal renderer is used. */ boolean useExternalRender(); - Set the external renderer listener
/** * Sets the external renderer listener. * @param onExternalRenderListener */ void setOnExternalRenderListener(OnExternalRenderListener onExternalRenderListener);public interface OnExternalRenderListener { public interface OnExternalRenderListener { /** * Notifies that a YUV data frame needs to be rendered. * @param width The width of the video frame. * @param height The height of the video frame. * @param timestamp The timestamp. Unit: ms. */ void onVideoFrameUpdate(int width, int height ,long timestamp); } - Retrieve YUV frame data for rendering
/** * Gets YUV frame data for rendering. This method is valid only in external rendering mode. * @return A video frame in YUV420p format. */ Yuv420pFrame getYuvFrame(); - Set the SEI information listener
/** * Sets the SEI information listener. * * @param seiInfoBuffer Creates a cache for storing SEI frame data. Make sure that the length of the SEI information in the stream does not exceed the cache size. Frames that exceed the size are discarded. * @param onSeiInfoListener */ void setOnSeiInfoListener(SeiInfoBuffer seiInfoBuffer, OnSeiInfoListener onSeiInfoListener); interface OnSeiInfoListener { /** * This method is called back when the SEI information is updated. * Do not perform blocking operations. Process the seiInfoBuffer data promptly in this callback. * @param seiInfoBuffer */ void onSeiInfoUpdate(SeiInfoBuffer seiInfoBuffer); } - Set the data source preparation event listener
/** * Sets the data source preparation event listener. * @param listener */ void setOnPreparedListener(OnPreparedListener listener); interface OnPreparedListener { /** * This method is called back when the data source is ready. */ void onPrepared(); } - Set the player error event listener
/** * Sets the player error event listener. For more information about error types, see: * {@link PlayerException.SOURCE_ERROR} * {@link PlayerException.RENDER_ERROR} * {@link PlayerException.UNEXPECTED_ERROR} * @param listener */ void setOnErrorListener(OnErrorListener listener); interface OnErrorListener { /** * This method is called back when a player error occurs. See * {@link PlayerException} * @param exception */ void onError(PlayerException exception); } - Set the player state change event listener
/** * Sets the player state change event listener. * @param listener */ void setOnPlayerStateChangedListener(OnPlayerStateChangedListener listener); interface OnPlayerStateChangedListener { /** * This method is called back when the player state changes. * * @param playerState See * {@link PlayerState#STATE_IDLE} * {@link PlayerState#STATE_BUFFERING} * {@link PlayerState#STATE_READY} * {@link PlayerState#STATE_ENDED} */ void onPlayerStateChange(int playerState); } - Set the first frame rendering event listener
/** * Sets the first frame rendering event listener. * @param listener */ void setOnRenderedFirstFrameListener(OnRenderedFirstFrameListener listener); interface OnRenderedFirstFrameListener { /** * This method is called back when the first frame is rendered. */ void onRenderedFirstFrame(); } - Set the video size change callback
/** * Sets the video size change callback. * @param listener */ void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener); interface OnVideoSizeChangedListener { /** * This method is called back when the content size changes. * * @param width The width of the video content. Unit: pixel. * @param height The height of the video content. Unit: pixel. */ void onVideoSizeChanged(int width, int height); } - Set the playback completion event listener
/** * Sets the playback completion event listener. * After this event is received, you need to call stop() to switch the player state to STATE_END. * @param listener */ void setOnCompletionListener(OnCompletionListener listener); interface OnCompletionListener { /** * This method is called back when playback reaches the end of the file. */ void onCompletion(); } - Obtain the current playback position
/** * Gets the current playback position, which is the offset from the start position. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @return Unit: ms. */ long getCurrentPosition(); - Obtain the total video duration
/** * Gets the total video duration. * This method is valid only when the player is in the {@link #STATE_READY} state. * @return Unit: ms. */ long getDuration(); - Obtain the volume
/** * Gets the volume. * @return The value ranges from 0 to 1. A value of 0 indicates silence. */ float getVolume(); - Obtain the player state
/** * Gets the playback state. * @return The state enumeration: * {@link PlayerState#STATE_IDLE} The initial state of the player. * {@link PlayerState#STATE_BUFFERING} The buffering state. * {@link PlayerState#STATE_READY} The state where buffering is complete and playback starts. * {@link PlayerState#STATE_ENDED} The playback completion state. */ int getPlayState(); - Obtain the connection type of the current stream
/** * Gets the connection type of the current stream. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @return {@link StreamConnectType} */ StreamConnectType getStreamConnectType(); - Obtain information about the current stream, such as the frame rate and bitrate
/** * Gets information about the current stream, such as frame rate and bitrate. * This method is valid when the player is in the {@link PlayerState#STATE_BUFFERING} or {@link PlayerState#STATE_READY} state. * @return A JSON string that contains information such as frame rate and bitrate. */ PlayInfo getCurrentPlayInfo();
- Constructor
- ExoHlsPlayer
- Constructor
ExoHlsPlayer(Context context); - Set the M3U8 playback address
/** * Sets the playback source. * @param url The M3U8 address. */ void setDataSource(String url); - Set the playback address to an IPC cloud recording (by filename)
/** * Sets the playback address to an IPC cloud recording (by filename). * @param iotId The iotId of the device. * @param fileName The name of the recording file. */ void setDataSourceByIPCRecordFileName(String iotId, String fileName); - Prepare the data source
/** * Checks and prepares data. */ void prepare(); - Start or resume video playback
/** * Starts or resumes video playback. */ void start(); - Pause playback
/** * Pauses playback. Call start() to resume playback. */ void pause(); - Seek to a specific position
/** * Seeks to a specific position. * @param positionInMs Milliseconds. */ void seekTo(long positionInMs); - Stop playback
** * Stops playback. */ void stop(); - Reset the player
/** * Resets the player. */ void reset(); - Release player resources
/** * Releases player resources. */ void release(); - Set whether to enable loop playback
/** * Sets whether to enable loop playback. * @param circlePlay true indicates that loop playback is enabled. */ void setCirclePlay(boolean circlePlay); - Set the playback speed
/** * Sets the playback speed. * @param speed The speed factor. The value ranges from (0, 2]. A value of 1 indicates the normal speed. */ void setPlaybackSpeed(float speed); - Set the player volume
/** * Sets the player volume. * @param audioVolume The value ranges from 0 to 1. A value of 0 indicates silence. */ void setVolume(float audioVolume); - Set the audio stream channel type
/** * Sets the audio stream channel type. See {@link android.media.AudioManager}. * If audio is playing, a brief pause may occur because the AudioTrack is recreated. * @param audioStreamType */ void setAudioStreamType(int audioStreamType); - Set the image scaling mode
/** * Sets the video image scaling mode. The default value is {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING}. * @param videoScalingMode See: * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT} * {@link android.media.MediaCodec#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} */ void setVideoScalingMode(int videoScalingMode); - Set the SurfaceView
/** * Sets the SurfaceView. * @param surfaceview */ void setSurfaceView(SurfaceView surfaceview); - Clear the SurfaceView
/** * Clears the SurfaceView. */ void clearSurfaceView(); - Set the TextureView
/** * Sets the TextureView. The same TextureView cannot be shared by multiple players. ZoomableTextureView with gesture-based scaling is provided for reference. * @param textureview */ void setTextureView(TextureView textureView); - Clear the TextureView
/** * Clears the TextureView. */ void clearTextureView(); - Set the data source preparation event listener
/** * Sets the data source preparation event listener. * @param listener */ void setOnPreparedListener(OnPreparedListener listener); interface OnPreparedListener { /** * This method is called back when the data source is ready. */ void onPrepared(); } - Set the player error event listener
/** * Sets the player error event listener. For more information about error types, see: * {@link PlayerException.SOURCE_ERROR} * {@link PlayerException.RENDER_ERROR} * {@link PlayerException.UNEXPECTED_ERROR} * @param listener */ void setOnErrorListener(OnErrorListener listener); interface OnErrorListener { /** * This method is called back when a player error occurs. See * {@link PlayerException} * @param exception */ void onError(PlayerException exception); } - Set the player state change event listener
/** * Sets the player state change event listener. * @param listener */ void setOnPlayerStateChangedListener(OnPlayerStateChangedListener listener); interface OnPlayerStateChangedListener { /** * This method is called back when the player state changes. * * @param playerState See * {@link PlayerState#STATE_IDLE} * {@link PlayerState#STATE_BUFFERING} * {@link PlayerState#STATE_READY} * {@link PlayerState#STATE_ENDED} */ void onPlayerStateChange(int playerState); } - Set the first frame rendering event listener
/** * Sets the first frame rendering event listener. * @param listener */ void setOnRenderedFirstFrameListener(OnRenderedFirstFrameListener listener); interface OnRenderedFirstFrameListener { /** * This method is called back when the first frame is rendered. */ void onRenderedFirstFrame(); } - Set the video size change callback
/** * Sets the video size change callback. * @param listener */ void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener); interface OnVideoSizeChangedListener { /** * This method is called back when the content size changes. * * @param width The width of the video content. Unit: pixel. * @param height The height of the video content. Unit: pixel. */ void onVideoSizeChanged(int width, int height); } - Obtain the current playback position
/** * Gets the current playback position, which is the offset from the start position. * This method is valid only when the player is in the {@link PlayerState#STATE_READY} state. * @return Unit: ms. */ long getCurrentPosition(); - Obtain the total video duration
/** * Gets the total video duration. * This method is valid only when the player is in the {@link #STATE_READY} state. * @return Unit: ms. */ long getDuration(); - Obtain the volume
/** * Gets the volume. * @return The value ranges from 0 to 1. */ float getVolume(); - Obtain the player state
/** * Gets the playback state. * @return The state enumeration: * {@link PlayerState#STATE_IDLE} The initial state of the player. * {@link PlayerState#STATE_BUFFERING} The buffering state. * {@link PlayerState#STATE_READY} The state where buffering is complete and playback starts. * {@link PlayerState#STATE_ENDED} The playback completion state. */ int getPlayState();
- Constructor
- ZoomableTextureView
- Set the maximum image scaling factor
/** * Sets the maximum image scaling factor. * The default value is 4. * * @param scale The factor. */ void setMaxScale(float scale); - Reset the image scaling factor to 1
/** * Resets the scaling factor to 1. * * @param smooth Specifies whether to enable a smooth transition effect. */ void zoomOut(boolean smooth); - Obtain the current scaling factor
/** * Gets the current scaling factor. * * @return The value is 1.0f when not scaled. */ float getScale(); - Set the listener
/* Sets the listener. * @param listener */ void setOnZoomableTextureListener(OnZoomableTextureListener listener); public interface OnZoomableTextureListener { /** * This method is called back when the image scaling factor changes. * * @param zoomableTextureView * @param scale The image scaling factor. */ void onScaleChanged(ZoomableTextureView zoomableTextureView, float scale); /** * This method is called back when the view is double-tapped. * * @param zoomableTextureView * @param e MotionEvent * @return Indicates whether the event is handled. If false is returned, the internal scaling logic is enabled. */ boolean onDoubleTap(ZoomableTextureView zoomableTextureView, MotionEvent e); /** * This method is called back when the view is single-tapped. * * @param zoomableTextureView * @param e MotionEvent * @return Indicates whether the event is handled. */ boolean onSingleTapConfirmed(ZoomableTextureView zoomableTextureView, MotionEvent e); /** * This method is called back when the view is long-pressed. * * @param zoomableTextureView * @param e MotionEvent */ void onLongPress(ZoomableTextureView zoomableTextureView, MotionEvent e); } - Set the edge listener
void setOnViewEdgeListener(OnViewEdgeListener listener) interface OnViewEdgeListener { /** * This method is called back when the edge of the view is touched for the first time. */ void onViewEdgeFirstTouched(); /** * This method is called back when the bottom edge of the view is dragged. The callback is continuously triggered if the drag continues. * * @param zoomableTextureView * @param delta The difference in touch movement relative to the last movement when continuously at the edge. */ void onBottomEdge(ZoomableTextureView zoomableTextureView, float delta); /** * This method is called back when the top edge of the view is dragged. The callback is continuously triggered if the drag continues. * * @param zoomableTextureView * @param delta The difference in touch movement relative to the last movement when continuously at the edge. */ void onTopEdge(ZoomableTextureView zoomableTextureView, float delta); /** * This method is called back when the right edge of the view is dragged. The callback is continuously triggered if the drag continues. * * @param zoomableTextureView * @param delta The difference in touch movement relative to the last movement when continuously at the edge. */ void onRightEdge(ZoomableTextureView zoomableTextureView, float delta); /** * This method is called back when the left edge of the view is dragged. The callback is continuously triggered if the drag continues. * * @param zoomableTextureView * @param delta The difference in touch movement relative to the last movement when continuously at the edge. */ void onLeftEdge(ZoomableTextureView zoomableTextureView, float delta); }
- Set the maximum image scaling factor
Errors
| Primary error code | Description | Sub-code | Description |
| SOURCE_ERROR | Data source-related errors | SUB_CODE_SOURCE_STREAM_CONNECT_ERROR(1005) | Failed to connect to the data source. |
| SUB_CODE_SOURCE_INVALID_DECRYPTE_KEY(1006) | Invalid decryption key. | ||
| SUB_CODE_SOURCE_INVALID_RTMP_URL(1007) | Invalid playback address. | ||
| SUB_CODE_SOURCE_PARAMETER_ERROR(1008) | Invalid data source parameters. | ||
| SUB_CODE_SOURCE_QUERY_URL_FAILED(1009) | Failed to request the playback address. | ||
| RENDER_ERROR | Rendering-related errors | SUB_CODE_RENDER_DECODE_ERROR(1000) | Decoding error. |
| UNEXPECTED_ERROR | Unexpected errors | SUB_CODE_UNEXPECTED_PULL_STREAM_ERROR(1100) | Failed to pull the stream. No stream was pulled within 8 seconds or the connection was unexpectedly disconnected. |
Voice intercom
This feature provides end-to-end one-way or two-way real-time intercom between the app and an IPC device.
- One-way talk: The app collects and sends audio data to the device for playback. The phone's speaker is muted while the app collects audio.
- Two-way talk: The app and the device collect and play audio at the same time. The device must support Acoustic Echo Cancellation (AEC). Otherwise, this solution is not recommended.
The following audio formats are supported.
| Type | Sample rate | Encoding | Decoding |
| G711A | 8 kHz/16 kHz | ✓ | ✓ |
| G711U | 8 kHz/16 kHz | ✓ | ✓ |
Usage guide
To integrate the voice intercom feature, perform the following steps.
- Create a voice intercom instance and set the intercom mode and audio parameters.
// Create a voice intercom instance. liveIntercomV2 = new LiveIntercomV2(context, iotId, LiveIntercomV2.LiveIntercomMode.DoubleTalk, AudioParams.AUDIOPARAM_MONO_8K_G711A); - Register a listener and handle voice intercom callbacks.
You must handle the corresponding event callbacks, such as the start of the intercom, the start and end of recording, and recording data callbacks. Recording data callbacks can be used for UI display, such as for volume calculation.
For more information about errors that may occur when you establish a voice channel or during an intercom, see Errors.
// Set the voice intercom error callback. liveIntercomV2.setLiveIntercomV2Listener(new LiveIntercomV2Listener() { @Override public void onTalkReady() { showToast("You can start talking now."); } @Override public void onError(LiveIntercomException error) { showToast("code:" + error.getCode() + " msg:" + error.getMessage()); } @Override public void onRecordStart() { // Mute the live player. player.setVolume(0f); showToast("Recorder started."); } @Override public void onRecordEnd() { // Restore the volume of the live player. player.setVolume(1f); showToast("Recorder stopped."); } @Override public void onRecordBufferReceived(byte[] buffer, int offset, int size) { // nothing Log.d(TAG, "onBufferReceived:" + size); } }); - Set the gain level.
You can set the gain value for audio collection on the app. Five levels are available: None, Low, Medium, High, and Strong. The default value is High. You can adjust the value based on the device's performance.
liveIntercomV2.setGainLevel(LiveIntercomV2.GAIN_LEVEL_NONE); - Start and stop the intercom.
After the intercom starts, the system requests audio focus and sets the mode to communication mode. The audio from the peer is routed to the speaker. If a Bluetooth headset or a wired headset is connected, the audio is routed to the headset. After the intercom ends, the audio focus is released, the mode is set to normal mode, and the rule that routes audio to the speaker is canceled. The echo cancellation feature of the mobile phone is enabled by default during the intercom.
// Start the intercom. liveIntercomV2.start(); // Manually stop the intercom. liveIntercomV2.stop();
API reference
- LiveIntercomV2
- Constructor
/** * Creates an intercom instance. * * @param context application context * @param iotId iotId * @param liveIntercomMode The intercom mode: one-way talk (SingleTalk) or two-way talk (DoubleTalk). * @param audioParams The audio parameters (sample rate, number of channels, sample bit width, and encoding format). */ LiveIntercomV2(Context context, final String iotId, final LiveIntercomMode liveIntercomMode, final AudioParams audioParams); - Start the voice intercom
/** * Starts the voice intercom. */ void start(); - Stop the voice intercom
/** * Stops the voice intercom, stops recording, closes the intercom channel, and stops playback. */ void stop(); - Release the intercom instance
/** * Releases the intercom instance. */ void release(); - Mute the intercom
/** * Sets mute. * * @param mute true indicates mute. false indicates unmute. */ void setMute(boolean mute) - Check if the current intercom is muted
/** * Checks if the current intercom is muted. * * @return */ boolean isMute() - Set the gain level
/** * Sets the gain level. * * @param gainLevel */ void setGainLevel(int gainLevel) - Set whether to enable support for Bluetooth and wired headsets
/** * Sets whether to enable support for Bluetooth and wired headsets. If enabled, peripherals are prioritized for recording and playback. This is enabled by default. * @param supportExternalHeadset Specifies whether to enable support. */ void setSupportExternalHeadset(boolean supportExternalHeadset); - Set and implement a third-party voice changing algorithm
/** * Sets the external implementation for voice changing. After setting, the {@link #setVoiceChangeType(LiveIntercomVoiceType)} and {@link #getLiveIntercomVoiceType()} APIs become invalid. * @param voiceChangeImpl The external API for voice changing. */ void setExternalVoiceChangeImpl(IVoiceChange voiceChangeImpl) interface IVoiceChange { /** * Changes the voice of the audio to be sent to the peer. This API does not allow blocking. * The audio sample rate, bit width, and number of channels must be consistent with those passed in {@link com.aliyun.iotx.linkvisual.media.audio.LiveIntercomV2#LiveIntercomV2(Context, String, LiveIntercomV2.LiveIntercomMode, AudioParams)}. * @param pcm The PCM data to be changed. Modify the content directly. The format is always PCM. * @param offset The offset. * @param length The data length. * @return Returns true if the operation is successful, false otherwise. */ boolean onChangeVoice(byte[] pcm, int offset, int length); } - Set the intercom listener
/** * Sets the intercom listener. * * @param listener */ void setLiveIntercomV2Listener(LiveIntercomV2Listener listener); interface LiveIntercomV2Listener { /** * The peer for the voice intercom is ready. */ void onTalkReady(); /** * An error occurred during the voice intercom. * @param error */ void onError(LiveIntercomException error); /** * Recording starts. */ void onRecordStart(); /** * Recording ends. */ void onRecordEnd(); /** * Receives recording data. * @param buffer * @param offset * @param size */ void onRecordBufferReceived(byte[] buffer, int offset, int size); }
- Constructor
Errors
| Error enumeration | Description |
| LiveIntercomException.INVALID_AUDIO_PARAMS | Invalid audio parameters for voice intercom. The audio parameters reported by the peer are not supported by the SDK. |
| LiveIntercomException.START_LIVE_INTERCOM_REQUEST_FAILED | Failed to start the voice intercom. |
| LiveIntercomException.CONNECTION_STREAM_FAILED | Failed to establish the voice stream. A timeout occurs if the stream is not established within 5 seconds. |
| LiveIntercomException.SEND_STREAM_DATA_FAILED | Failed to send voice stream data. |
| LiveIntercomException.RECEIVE_STREAM_DATA_FAILED | Failed to receive voice stream data. |
| LiveIntercomException.INIT_RECORD_FAILED | Failed to initialize the recorder. |
| LiveIntercomException.START_RECORD_FAILED | Failed to start the recorder. |
| LiveIntercomException.READ_RECORD_BUFFER_FAILED | Failed to read recording data. |
| LiveIntercomException.INIT_AUDIO_PLAYER_FAILED | Failed to create the audio player. |