入门篇 - 获取麦克风

06/09 11:23
阅读数 34

来源于:https://yunp.top/app/index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="vue.js"></script>
</head>
<body>
<div id="vueapp">
    <div>
        <audio controls ref="audio"></audio>
    </div>
    <div>
        <select v-model="selectedAudioDeviceIndex">
            <option v-for="(d,index) in audioInputDevices" :value="index">{{d.label}}</option>
        </select>
    </div>
</div>
<script src="app.js"></script>
</body>
</html>
(async function () {
    // let devices = await navigator.mediaDevices.enumerateDevices();
    //
    // console.log(devices);

    let stream = await navigator.mediaDevices.getUserMedia({video: false, audio: true});
    document.querySelector("audio").srcObject = stream;
})();


new Vue({
    el: "#vueapp",
    data: {
        audioInputDevices: [],
        selectedAudioDeviceIndex: 0
    },

    mounted() {
        this._initVueApp();
    },


    methods: {

        async _initVueApp() {
            let devices = await navigator.mediaDevices.enumerateDevices();
            console.log(devices);
            let audioInputDevices = devices.filter(value => value.kind === 'audioinput');
            console.log(audioInputDevices);
            this.audioInputDevices.length = 0;
            this.audioInputDevices.push(...audioInputDevices);

            this.showSelectedDevice();
        },

        async showSelectedDevice() {
            let deviceInfo = this.audioInputDevices[this.selectedAudioDeviceIndex];
            let stream = await navigator.mediaDevices.getUserMedia({video: false, audio: deviceInfo});
            this.$refs.audio.srcObject = stream;
        }
    },


    watch: {
        selectedAudioDeviceIndex(val, oldVal) {
            this.showSelectedDevice();
        }
    }
});
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部