2017-08-29 38 views
1

我正在研究Twilio視頻應用程序,並且存在訪問通過地圖提供的遠程參與者視頻/音頻軌道的問題。使用開發工具玩弄我已經告訴我,我需要的值應該可以通過我正在使用的方法來訪問,但它似乎並不工作。
的設置了房間的代碼:
在Javascript地圖中訪問值的問題,函數執行順序可能存在的問題

function connectVid(){ 
Twilio.Video.connect(localStorage.vidToken, { 
    audio: true, 
    video: {width: 520} 
}).then(function(room){ 
    window.room = room; 
    console.log('Connected to Room: ', room.name); 
    Twilio.Video.createLocalTracks().then(function(localTracks) { 
     console.log('Creating tracks in Room: '); 
     console.log(room); 
     console.log('Got default audio and video tracks: ', localTracks); 
     var localParticipant = room.localParticipant; 
     trackArray = localTracks; 
     console.log(trackArray); 
     console.log('Connected to the Room as localParticipant "%s"', localParticipant.identity); 
     var localMediaContainer = document.getElementById('lstream'); 
     localTracks.forEach(function(track) { 
      localMediaContainer.appendChild(track.attach()); 
     }); 
    }) 
}).then(function(room){ 
    attachRemoteParticipant(); 
}).catch(function(err){ 
    console.log(err.message); 
    if(err.code === 20104) { 
     getVidToken(); 
    }; 
}) 
}; 


的attachRemoteParticipant();代碼:

function attachRemoteParticipant() { 
room.participants.forEach(function(participant) { 
    console.log('Participant "%s" is connected to the Room', participant.identity); 
    var remoteContainer = document.getElementById('rstream'); 
    var remoteTracks = Array.from(participant.tracks.values()); 
    console.log("Remote Tracks COMING:"); 
    console.log(remoteTracks); 
    remoteTracks.forEach(function(track){ 
     console.log('In Remote 4 each'); 
     console.log(track); 
     remoteContainer.appendChild(track.attach()); 
    }); 
}); 
}; 


我試圖包括attachRemoteParticipant功能connectVid()代碼內未抽象,在所述第一然後(函數(...){...})的端部作爲connectVid外呼,應事後發生像這樣:

$('#new-twilio-video').click(function(){ 
    if(localStorage.vidToken == undefined) { 
     getVidToken(); 
     // attachRemoteParticipant(); 
    } else { 
     connectVid(); 
     // attachRemoteParticipant(); 
    } 
    addVidModal(); 
}); 


未註釋的,當然。用console.log('Participant "%s" is connected to the Room', participant.identity);記錄的是正確的,但console.log(remoteTracks);產生一個空數組。但是,如果我使用開發工具運行room.participants.forEach(function(participant) {console.log(Array.from(participant.tracks.values());})我收到正是我想要的。
我認爲這個問題與值準備提取之前發生的Array.from(...)調用有關,因此我試圖阻止早期執行,但我可能是錯的。任何建議,將不勝感激。
編輯:開發工具提供我想要的數組,但也作爲第二個結果undefined。會導致問題嗎?

回答

0

出於某種原因,我必須調用我的方法訪問setTimeout後的地圖值。不知何故,在console.log中出現的值直到以後才能訪問。我找不出一個優雅的方式來做到這一點(使用普通的承諾和異步/等待不起作用),但良好的'setTimeout做了訣竅。