vue实现多个摄像头和麦克风下拉选择进行拍照拍摄视频和录音预览并下载到本地代码

代码语言:html

所属分类:多媒体

代码描述:vue实现多个摄像头和麦克风下拉选择进行拍照拍摄视频和录音预览并下载到本地代码先列出本机的摄像头和麦克风下拉列表选择,然后三个按钮,一个是拍照,一个录视频、一个是录音,拍照录制的图片、声音、视频可以一键下载到本地,录制完成可在先预览。

代码标签: css 小方块 背景 图案

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1-dev.js"></script>
</head>
<body>
  <div id="app">
    <!-- 摄像头选择下拉菜单 -->
    <div>
      <label for="videoDevices">选择摄像头:</label>
      <select id="videoDevices" v-model="selectedVideoDevice">
        <option v-for="device in videoDevices" :key="device.deviceId" :value="device.deviceId">
          {{ device.label || 'Device ' + (videoDevices.indexOf(device) + 1) }}
        </option>
      </select>
    </div>

    <!-- 麦克风选择下拉菜单 -->
    <div>
      <label for="audioDevices">选择麦克风:</label>
      <select id="audioDevices" v-model="selectedAudioDevice">
        <option v-for="device in audioDevices" :key="device.deviceId" :value="device.deviceId">
          {{ device.label || 'Device ' + (audioDevices.indexOf(device) + 1) }}
        </option>
      </select>
    </div>

    <!-- 预览区域 -->
    <div>
      <label>预览:</label>
       <img ref="previewImg"/>
      <video ref="previewVideo" autoplay playsinline controls></video>
      <audio ref="previewAudio" controls></audio>
    </div>

    <!-- 拍照按钮 -->
    <button @click="takePhoto">拍照</button>

    <!-- 录视频按钮 -->
    <button @click="recordVideo">{{ isRecordingVideo ? '停止录制' : '录制视频' }}</button>

    <!-- 录音按钮 -->
    <button @click="recordAudio">{{ isRecordingAudio ? '停止录制' : '录制音频' }}</button>

    <!-- 下载按钮 -->
    <a v-if="mediaBlob" :href="mediaBlobUrl" download="mediafile">{{ downloadButtonText }}</a>
  </div>

  <script>
    var app = new Vue({
      el: '#app',
      data: {
        videoDevices: [],
        audioDevices: [],
        selectedVideoDevice: null,
        selectedAudioDevice: null,
        isRecordingVideo: false,
        isRecordingAudio: false,
        mediaBlob: null,
        mediaBlobUrl: '',
        
        downloadButtonText: ''
      },
      methods: {
        // 获取可用的摄像头和麦克风设备
        getAvailableDevices: function() {
          navigator.mediaDevices.enumerateDevices()
            .then(devices => {
              devices.forEach(device => {
                if (device.kind === 'videoinput') {
                  this.videoDevices.push(device);
                } else if (device.kind === 'audioinput') {
                  this.audioDevices.push(device);
                }
              });
            })
            .catch(error => {
              console.error('获取设备失败:', error);
            });
        },
        // 拍照功能
        takePhoto: function() {
          var constraints = {
            video: { deviceId: this.selectedVideoDevice }
          };
          navigator.mediaDevices.getUserMedia(constraints)
            .then(stream => {
              var videoTracks = stream.getVideoTracks();
              var videoTrack = videoTracks[0];
              var imageCapture = new ImageCapture(videoTrack);
              return imageCapture.takePhoto();
            })
            .then(blob => {
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0