tencent cloud


Publishing from Screen

Last updated: 2024-01-13 15:49:41


    Publishing from screen means using hosts’ phones as the source for live streaming. It may be combined with local camera preview and is used in scenarios such as game streaming and mobile application demonstration. Tencent Cloud’s LiteAVSDK offers the screen sharing capability via V2TXLivePusher.
    By adding a floating window to display the image of the local camera, you can include camera preview into the streams published from the screen.


    Screen recording is supported in Android 5.0 and above.
    Floating windows need to be enabled manually on some mobile phones and systems.

    Sample Code


    Step 1. Download SDK development kit

    Download the SDK and follow the instructions in SDK Integration to integrate the SDK into your application.

    Step 2. Configure License Authorization for SDK

    1. Obtain license authorization:
    If you have obtained the relevant license authorization,Need to Get License URL and License Key in Cloud Live Console
    If you have not yet obtained the license authorization,Please reference Adding and Renewing Licenses to make an application.
    2. Before your App calls SDK-related functions (it is recommended in the Application class), set the following settings:
    public class MApplication extends Application {
    public void onCreate() {
    String licenceURL = ""; // your licence url
    String licenceKey = ""; // your licence key
    V2TXLivePremier.setEnvironment("GDPR"); // set your environment
    V2TXLivePremier.setLicence(this, licenceURL, licenceKey);
    V2TXLivePremier.setObserver(new V2TXLivePremierObserver() {
    public void onLicenceLoaded(int result, String reason) {
    Log.i(TAG, "onLicenceLoaded: result:" + result + ", reason:" + reason);
    The packageName configured in the license must be the same as the application itself, otherwise the push stream will fail.

    Step 3. Add Activity

    Paste the following activity in the manifest file (no need to add it if it exists in the project code).

    Step 4. Create a stream publishing object

    Create a V2TXLivePusher object, which will be responsible for publishing operations.
    // Specify the corresponding live broadcast protocol as RTMP, which does not support mic connection
    V2TXLivePusher mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);

    Step 5. Start stream publishing

    Use startScreenCapture to start screen recording,and use V2TXLivePusher::startPush to pushing
    if you chooseRTMPprotocol to push in Step4,The generate of the push URL, please refer to RTMP URL
    // The push stream can be started according to the push stream protocol. RTMP cannot connect to the microphone. It should use rtmp:// to connect to the microphone.
    String url = "rtmp://test.com/live/streamid?txSecret=xxxxx&txTime=xxxxxxxx";
    int ret = mLivePusher.startPush(url);
    Log.i(TAG, "startRTMPPush: license verification failed");
    Reason for returning V2TXLIVE_ERROR_INVALID_LICENSE? If the startPush interface returns V2TXLIVE_ERROR_INVALID_LICENSE, it means that your license verification failed, please check the url and key set in Step 2: Configure the SDK for license authorization.
    startScreenCapture is used to start screen recording, which is a built-in feature of the Android system. For security reasons, before screen recording starts, Android will pop up a window asking users whether to start screen recording, and you should agree.

    Step 6. Set watermarks

    Call setWatermark in V2TXLivePusher to add a watermark to videos output by the SDK. The position of the watermark is determined by the (x, y, scale) parameter passed in.
    The watermark image must be in PNG rather than JPG format. The former carries opacity information, which allows the SDK to better address the image aliasing issue (changing the extension of a JPG image to PNG won’t work).
    The (x, y, scale) parameter specifies the normalized coordinates of the watermark relative to the resolution of the published video. For example, if the resolution of the published video is 540 x 960, and (x, y, scale) is set to (0.1, 0.1, 0.1), the actual pixel coordinates of the watermark will be (540 x 0.1, 960 x 0.1). The width of the watermark will be the video width x 0.1, and the height will be scaled automatically.
    // Set a video watermark
    mLivePusher.setWatermark(BitmapFactory.decodeResource(getResources(),R.drawable.watermark), 0.03f, 0.015f, 1f);

    Step 7. Set video quality

    Call setVideoQuality in V2TXLivePusher to set the quality of videos watched by audience. The encoding parameters set determine the quality of videos presented to audience. The local video watched by the host is the original HD version that has not been encoded or compressed, and is therefore not affected by the settings. For details, please see Setting Video Quality.

    Step 8. Inform hosts of poor network conditions

    Connecting phones to Wi-Fi does not necessarily guarantee network conditions. In case of poor Wi-Fi signal or limited bandwidth, the network speed of a Wi-Fi connected phone may be slower than that of a phone using 4G. Hosts should be informed when their network conditions are bad and be prompted to switch to a different network.
    You can capture the V2TXLIVE_WARNING_NETWORK_BUSY event using onWarning in V2TXLivePusherObserver. The event indicates poor network conditions for hosts, which result in stuttering for audience. When this event occurs, you can send a UI message about poor network conditions to hosts, as shown above.
    public void onWarning(int code, String msg, Bundle extraInfo) {
    if (code == V2TXLiveCode.V2TXLIVE_WARNING_NETWORK_BUSY) {
    showNetBusyTips(); // Show a “network busy” message

    Step 9. Set the orientation

    In most cases, hosts stream while holding their phones vertically, and audience watch videos in portrait resolutions (e.g., 540 × 960). However, there are also cases where hosts hold phones horizontally, and ideally, audience should watch videos in landscape resolutions (960 × 540), as shown below:
    By default, V2TXLivePusher outputs videos in portrait resolutions. You can output landscape-mode videos to audience by modifying a parameter of the setVideoQuality API.
    mLivePusher.setVideoQuality(mVideoResolution, isLandscape ? V2TXLiveVideoResolutionModeLandscape : V2TXLiveVideoResolutionModePortrait);

    Step 10. Stop publishing streams

    As there can be only one V2TXLivePusher object running at a time, make sure that you release all the resources when stopping publishing.
    // Stop screen sharing and release the resources
    public void stopPublish() {

    Event Handling

    Listening for events

    The SDK listens for publishing events and errors via the V2TXLivePusherObserver delegate. See V2TXLiveCode for a detailed list of events and error codes.


    An error indicates that the SDK encountered a serious problem that made it impossible for stream publishing to continue.
    Event ID
    A common error not yet classified
    An invalid parameter was passed in during API calling.
    The API call was rejected.
    The API cannot be called.
    Failed to call the API due to invalid license.
    The server request timed out.
    The server could not handle your request.


    A warning indicates that the SDK encountered a problem whose severity level is warning. Warning events trigger tentative protection or recovery logic and can often be resolved.
    Event ID
    Bad network connection: data upload blocked due to limited upstream bandwidth.
    Stuttering during video playback.
    Failed to turn the camera on.
    The camera is occupied. Try a different camera.
    No access to the camera. This usually occurs on mobile devices and may be because the user denied the access.
    Failed to turn the mic on.
    The mic is occupied. This occurs when, for example, the user is having a call on the mobile device.
    No access to the mic. This usually occurs on mobile devices and may be because the user denied the access.
    The system does not support screen sharing.
    Failed to start screen recording. If this occurs on a mobile device, it may be because the user denied the access.
    Screen recording was stopped by the system.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support