我目前正在撰写使用Chrome's DesktopCapture API的Chrome扩展程序。当有人点击“停止共享”时,我很难设置回调。
我尝试使用MediaStream的onended
EventHandler,但点击按钮后,MediaStream的ended
属性仍然设置为true
。
我可以在流(点击按钮之前和之后)之间找到的唯一区别是videoTracks.readyState
设置为ended
。
编辑: 我还想注意用户是否关闭了之前流式传输的窗口。
答案 0 :(得分:15)
我通过在 videoTrack的 onended
属性上分配一个EventHandler解决了这个问题:
// somebody clicked on "Stop sharing"
stream.getVideoTracks()[0].onended = function () {
// doWhatYouNeedToDo();
};
就我的编辑而言(注意到关闭窗口):它也会触发事件。
答案 1 :(得分:0)
stream.onended = () => { // Click on browser UI stop sharing button
console.info("Recording has ended");
};
从哪里获得流?使用adapter.js(跨浏览器制作基于Promise的通用API):
navigator.mediaDevices.getUserMedia(..).then((stream) => {..});
答案 2 :(得分:0)
现在我们处于非活动状态,您可以像这样使用
uses
System.Win.ScktComp, Vcl.Imaging.jpeg;
type
TForm1 = class(TForm)
Button1: TButton;
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Port := 1234;
ServerSocket1.Active := True;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s: string;
Stream: TMemoryStream;
Receiving: Boolean;
stSize: Integer;
jpg: TJpegImage;
begin
if Socket.ReceiveLength > 0 then
begin
s := Socket.ReceiveText;
if not Receiving then
begin
if Pos(#0, s) > 0 then
begin
stSize := strToInt(Copy(s, 1, Pos(#0, s) - 1));
ShowMessage(IntToStr(Round(stSize / 1024)));
end
else
;
Stream := TMemoryStream.Create;
Receiving := true;
Delete(s, 1, Pos(#0, s));
end;
try
Stream.Write(AnsiString(s)[1], length(s));
if Stream.Size = stSize then
begin
Stream.Position := 0;
Receiving := false;
jpg := TJpegImage.Create;
jpg.LoadFromStream(Stream);
jpg.SaveToFile('received.jpg');
Stream.Free;
end;
except
Stream.Free;
end;
end;
end;
end.