Polyv Help Center

Help Center

7 Video Screen Casting

Updated: 2026-05-21 18:34:06

The screen casting feature is used to cast on-demand videos to large-screen devices for playback. In the old on-demand Android SDK, the screen casting capability was provided by the Demo layer. Developers can refer to the screen casting module source code in the Demo to integrate it into their own projects.

7.1 Applicable Versions and Limitations

  • Since on-demand Android SDK 2.15.0, the screen casting SDK has been moved to the Demo layer. Developers need to import the screen casting module source code from the Demo themselves.
  • Since Demo 2.24.2, the screen casting service has been replaced with a self-developed screen casting service.
  • The examples in this document primarily focus on non-encrypted video casting. The encrypted video casting capability from the old screen casting documentation is not covered in these examples.
  • The screen casting feature is unavailable when the screen recording prevention capability is enabled.
  • The screen casting sender and receiver must be on a mutually reachable network, and the receiver must support the corresponding screen casting capability.

7.2 Integrating Screen Casting Dependencies

Add the screen casting dependency to the project's build.gradle:

// 投屏 SDK
implementation "net.polyv.android:media-cast-adapter-rx:1.0.5"

7.3 Importing Demo Screen Casting Source Code

Import the screen casting related source code from the Demo's com/easefun/polyvsdk/cast directory into the project.

// com/easefun/polyvsdk/cast
├── IPLVScreencastListener.java // 投屏播放状态监听器
├── PolyvAllCast.java // 自研投屏能力封装类
├── PolyvIUIUpdateListener.java // UI 状态回调监听器
├── PolyvScreencastManager.java // 投屏功能管理类
└── widget
    ├── PolyvScreencastSearchLayout.java // 搜索投屏设备的视图
    └── PolyvScreencastStatusLayout.java // 投屏状态管理视图

Among them, PolyvScreencastManager is the main screen casting management class used by the business side, encapsulating device discovery, casting playback, playback control, and status callbacks.

7.4 Initializing the Screen Casting Manager

Obtain the PolyvScreencastManager instance on the playback page and set the status callback.

private PolyvScreencastManager screencastManager;

private void initScreencastManager() {
    screencastManager = PolyvScreencastManager.getInstance(this);
    screencastManager.setUIUpdateListener(new PolyvIUIUpdateListener() {
        @Override
        public void onUpdateState(int state, Object object) {
            // 根据 state 更新设备搜索、播放状态、播放进度或错误提示
        }

        @Override
        public void onUpdateText(String msg) {
            // 展示投屏提示文案或错误信息
        }
    });

    screencastManager.initService();
}

7.5 Searching for Screen Casting Devices

Call browse() to start searching for devices. After the search ends, the currently scanned device list can be obtained via getInfos().

// 开始搜索设备
screencastManager.browse();

// 获取搜索到的设备列表
List<PLVMediaCastDevice> devices = screencastManager.getInfos();

Search results are returned via the PolyvIUIUpdateListener#onUpdateState callback. Common statuses include:

Status Description
STATE_SEARCH_SUCCESS Device search successful
STATE_SEARCH_ERROR Device search failed
STATE_SEARCH_NO_RESULT No available devices found

7.6 Initiating Screen Casting Playback

After selecting a target device from the search results, call playNetMedia(device, resource, startSeconds) to initiate screen casting playback.

// device:从 getInfos() 获取的投屏设备
// mediaResource:投屏播放资源,请按 Demo 中当前视频信息构建
// startSeconds:起播位置,单位为秒
screencastManager.playNetMedia(device, mediaResource, startSeconds);

After initiating casting, the currently connected screen casting device can be obtained via getConnectInfos().

PLVMediaCastDevice currentDevice = screencastManager.getConnectInfos();

7.7 Playback Control

Once screen casting playback has started, the sender can control the playback state of the receiver.

// 暂停
screencastManager.pause();

// 恢复播放
screencastManager.resume();

// 跳转到指定进度,单位为秒
screencastManager.seekTo(progress);

// 设置音量,范围 0~100
screencastManager.setVolume(percent);

// 增加音量
screencastManager.volumeUp();

// 减少音量
screencastManager.volumeDown();

// 停止投屏播放
screencastManager.stopPlay();

7.8 Monitoring Playback Status

Screen casting playback status, playback progress, and error information are returned via PolyvIUIUpdateListener#onUpdateState. Common statuses include:

Status Description
STATE_PLAY Screen casting is playing
STATE_PAUSE Screen casting is paused
STATE_STOP Screen casting has stopped
STATE_COMPLETION Screen casting playback completed
STATE_POSITION_UPDATE Screen casting playback progress update, object is the current progress
STATE_PLAY_ERROR Screen casting playback error, object is the error message
STATE_LOADING Screen casting is loading

7.9 Releasing Screen Casting Resources

When the playback page exits or the screen casting feature is no longer needed, call release() to release screen casting resources.

@Override
protected void onDestroy() {
    if (screencastManager != null) {
        screencastManager.release();
    }
    super.onDestroy();
}

7.10 Frequently Asked Questions

Why can't I cast when screen recording prevention is enabled?

The screen recording prevention capability conflicts with the screen casting capability. When screen recording prevention is enabled, the screen casting feature is unavailable.

What should I do if no screen casting devices are found?

Please ensure that the sender and receiver are on a mutually reachable network, and confirm that the receiver supports the corresponding screen casting capability. If devices still cannot be found, refer to the device search logic in the Demo's PolyvScreencastSearchLayout for troubleshooting.

Is it mandatory to use the Demo's screen casting UI?

No. The Demo provides a device search view and a screen casting status view. Developers can reuse them directly or implement their own UI based on business needs. If implementing a custom UI, it is recommended to retain the search, playback control, and status callback logic of PolyvScreencastManager.

联系客服,在线咨询