This question already has an answer here:
WebRTC video is not displaying

(1个答案)


去年关闭。




我试图将使用getusermedia()捕获的流附加到startPeerConnection(stream)上。我试图以以下方式添加流。
   function startPeerConnection(stream) {
            var configuration = {
                // Uncomment this code to add custom iceServers    //
                "iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
            };

            yourConnection = new RTCPeerConnection(configuration);
            theirConnection = new RTCPeerConnection(configuration);


            // Setup stream listening
            yourConnection.addStream(stream);//***getting the error on this line***
            theirConnection.onaddstream = function (e) {
                theirVideo.srcObject = e.stream;
                theirVideo.play();
            };


            // Setup ice handling
            yourConnection.onicecandidate = function (event) {
                if (event.candidate) {
                    theirConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
                }
            };

            theirConnection.onicecandidate = function (event) {
                if (event.candidate) {
                    yourConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
                }
            };



            // Begin the offer
            yourConnection.createOffer(function (offer) {
                yourConnection.setLocalDescription(offer);
                theirConnection.setRemoteDescription(offer);
                theirConnection.createAnswer(function (offer) {
                    theirConnection.setLocalDescription(offer);
                    yourConnection.setRemoteDescription(offer);
                });
            });
        };


RTCpeerconnection是这样的:
var RTCPeerConnection = function(options) {

    var iceServers = options.iceServers || defaults.iceServers;
    var constraints = options.constraints || defaults.constraints;

    var peerConnection = new PeerConnection(iceServers);

    peerConnection.onicecandidate = onicecandidate;
    peerConnection.onaddstream = onaddstream;
    peerConnection.addStream(options.stream);//***getting error on here ***

    function onicecandidate(event) {
        if (!event.candidate || !peerConnection) return;
        if (options.getice) options.getice(event.candidate);
    }

    function onaddstream(event) {
        options.gotstream && options.gotstream(event);
    }


最佳答案

addStream是一种已从标准中删除的方法,而Safari并未实现。
通过替换切换到addTrack方法

peerConnection.addStream(options.stream);


options.stream.getTracks().forEach(track => peerConnection.addTrack(track, options.stream))

或将adapter.js包含在您的项目中,以polyfill填充addStream

关于javascript - 未捕获的TypeError:peerConnection.addstream不是函数吗? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57106846/

10-11 08:10