入门篇

橙三吉。 提交于 2020-10-04 04:26:59

来源于: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();
        }
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!