我正在使用 agora io 进行视频通话和屏幕共享。屏幕共享工作正常。但问题是当我取消屏幕共享时,流不显示。但加入仍然有效。我需要捕捉屏幕共享的取消事件。我已阅读文档,但没有关于取消事件的线索。您能帮忙取消活动吗?提前谢谢。
问问题
2412 次
4 回答
4
If you are using agora web sdk ng library then you can detect this by using track-ended event.
We can create screen track using createScreenVideoTrack in this sdk.
step-1 call this method to create track
async shareScreen() {
const localScreenTrack = await AgoraRTC.createScreenVideoTrack();
return localScreenTrack;
}
step2- call event method after creating track.
To add event on this track create different method to handle event
shareScreenEvent(){
const track = this.shareScreen()
if (track) {
track.on('track-ended', () => {
console.log('track-ended');
console.log('you can run your code here to stop screen')
})
}
}
于 2021-01-21T12:16:40.217 回答
3
如果你使用 Agora Web SDK NG,那么你可以这样使用——
screen_rtc.screenTrack.close();
这里 screen_rtc 是我的客户端对象,您也可以禁用屏幕客户端,如下所示 -
screen_rtc.client.leave();
我的屏幕共享完整代码如下 -
<!-- Import Agora -->
import AgoraRTC from "agora-rtc-sdk-ng";
<!-- Create client for screen -->
screen_rtc: {
client: null,
screenTrack: null,
},
<!-- Start Screen sharing -->
screen_rtc.client = AgoraRTC.createClient({
mode: "rtc",
codec: "vp8",
});
await screen_rtc.client.join(
<YOUR_APP_ID>,
<YOUR_CHANNEL_NAME>,
<YOUR_TOKEN>,
null
);
screen_rtc.screenTrack = await AgoraRTC.createScreenVideoTrack();
await screen_rtc.client.publish(screen_rtc.screenTrack);
<!-- Stop screen sharing -->
screen_rtc.screenTrack.close();
await screen_rtc.client.leave();
于 2020-10-03T09:20:56.027 回答
0
这是一个错误,您应该处理它。如果用户取消屏幕共享,屏幕轨道将需要停止并离开。因此,您可以简单地使用 try/catch。
try {
await clientScreen.join(appId, channelName, rtcToken, screenShareUid);
const screenTrack = await AgoraRTC.createScreenVideoTrack(
{
encoderConfig: interviewInfo.video_encoding[0] as VideoEncoderConfiguration,
},
'disable'
);
screenTrack.on('track-ended', () => {
handleStopShareScreen();
});
if (screenTrack) {
await clientScreen.publish(screenTrack);
}
} catch (e) {
handleStopShareScreen();
}
};
和 handleStopShareScreen 函数:
const handleStopShareScreen = async () => {
if (screenShareTrack) {
screenShareTrack.stop();
screenShareTrack.close();
}
clientScreen.removeAllListeners();
await clientScreen.leave();
};
于 2022-02-22T07:59:30.963 回答
0
Agora 的 Web SDK 不提供任何方法来检测这一点,但由于 Agora Web SDK 是基于 Web RTC 的,因此您可以利用navigator
对象和catch
错误。
navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {
/* media access has been granted */
}).catch(function(err) {
//log to console first
console.log(err); /* handle the error */
if (err.name == "NotFoundError" || err.name == "DevicesNotFoundError") {
//required track is missing
} else if (err.name == "NotReadableError" || err.name == "TrackStartError") {
//webcam or mic are already in use
} else if (err.name == "OverconstrainedError" || err.name == "ConstraintNotSatisfiedError") {
//constraints can not be satisfied by avb. devices
} else if (err.name == "NotAllowedError" || err.name == "PermissionDeniedError") {
//permission denied in browser
} else if (err.name == "TypeError" || err.name == "TypeError") {
//empty constraints object
} else {
//other errors
console.log("Error: " + err.name);
}
});
参考链接:https
://blog.addpipe.com/common-getusermedia-errors/
代码笔演示:https ://codepen.io/naicuoctavian/pen/wPeZWO
于 2019-10-02T22:54:05.273 回答