-
-
Save n0bisuke/9f818509a1a44b45e8a0b4800a23848d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strcit'; | |
const APIKEY = 'APIキー'; | |
const app = new Vue({ | |
el: '#app', | |
data: { | |
audios: [], | |
videos: [], | |
selectedAudio: '', | |
selectedVideo: '', | |
peerId: '', | |
}, | |
methods: { | |
onChange: function(){ | |
if(this.selectedAudio != '' && this.selectedVideo != ''){ | |
this.connectLocalCamera(); | |
} | |
}, | |
connectLocalCamera: async function(){ | |
const constraints = { | |
audio: this.selectedAudio ? { deviceId: { exact: this.selectedAudio } } : false, | |
video: this.selectedVideo ? { deviceId: { exact: this.selectedVideo } } : false | |
} | |
const stream = await navigator.mediaDevices.getUserMedia(constraints); | |
document.getElementById('my-video').srcObject = stream; | |
}, | |
}, | |
mounted: async function(){ | |
this.peer = new Peer({key: APIKEY, debug: 3}); //新規にPeerオブジェクトの作成 | |
this.peer.on('open', () => this.peerId = this.peer.id); //PeerIDを反映 | |
//デバイスへのアクセス | |
const deviceInfos = await navigator.mediaDevices.enumerateDevices(); | |
//オーディオデバイスの情報を取得 | |
deviceInfos | |
.filter(deviceInfo => deviceInfo.kind === 'audioinput') | |
.map(audio => this.audios.push({text: audio.label || `Microphone ${this.audios.length + 1}`, value: audio.deviceId})); | |
//カメラの情報を取得 | |
deviceInfos | |
.filter(deviceInfo => deviceInfo.kind === 'videoinput') | |
.map(video => this.videos.push({text: video.label || `Camera ${this.videos.length - 1}`, value: video.deviceId})); | |
console.log(this.audios, this.videos); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<div id="app"> | |
<video id="my-video" muted="true" width="500" autoplay playsinline></video> | |
<p>Your Peer ID: <span id="my-id">{{peerId}}</span></p> | |
<br /> | |
マイク: | |
<select v-model="selectedAudio" @change="onChange"> | |
<option disabled value="">Please select one</option> | |
<option v-for="(audio, key, index) in audios" v-bind:key="index" :value="audio.value"> | |
{{ audio.text }} | |
</option> | |
</select> | |
カメラ: | |
<select v-model="selectedVideo" @change="onChange"> | |
<option disabled value="">Please select one</option> | |
<option v-for="(video, key, index) in videos" v-bind:key="index" :value="video.value"> | |
{{ video.text }} | |
</option> | |
</select> | |
</div> | |
<script src="https://cdn.webrtc.ecl.ntt.com/skyway-latest.js"></script><!-- 追記 --> | |
<script src="https://jp.vuejs.org/js/vue.min.js"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment