vue3 使用recorder-core 实现在线录制音频

本文介绍如何在Vue3项目中利用recorder-core库进行在线音频录制。提供了项目所需的代码片段,并给出了获取完整源代码的Gitee链接。
摘要由CSDN通过智能技术生成

vue3 使用recorder-core 实现在线录制音频
网址https://gitee.com/xiangyuecn/Recorder
gitee:https://gitee.com/xiangyuecn/Recorder.git

代码,样式一:

<template>
  <div class="learnDetail">
    <!-- 音频录制 -->
    <div class="main">
      <div class="mainBox">
        <div class="btns">
          <div>
            <button @click="recOpen">打开录音,请求权限</button>
            <button @click="recClose">关闭录音,释放资源</button>
          </div>

          <button @click="recStart">录制</button>
          <button @click="recStop" style="margin-right: 80px">停止</button>

          <span style="display: inline-block">
            <button @click="recPause">暂停</button>
            <button @click="recResume">继续</button>
          </span>
          <span style="display: inline-block">
            <button @click="recPlayLast">播放</button>
            <button @click="recUploadLast">上传</button>
          </span>
        </div>
      </div>

      <div class="mainBox">
        <!-- 录音波形 -->
        <div class="ctrlProcessWave noneWrap" />
        <!-- 录音频率计时 -->
        <div class="haveWrap">
          <div
            class="ctrlProcessX"
            style="height: 40px; background: #0b1; position: absolute"
            :style="{ width: state.powerLevel + '%' }"
          ></div>
          <div class="ctrlProcessT" style="padding-left: 50px; line-height: 40px; position: relative">
            {
   {
    state.duration + '/' + state.powerLevel }}
          </div>
        </div>
      </div>

      <div class="mainBox">
        <audio ref="LogAudioPlayer" style="width: 100%"></audio>
        <!-- 日志 -->
        <div class="mainLog">
          <div v-for="obj in state.logs" :key="obj.idx">
            <div :style="{ color: obj.color == 1 ? 'red' : obj.color == 2 ? 'green' : obj.color }">
              <span v-once>[{
   {
    getTime() }}]</span><span v-html="obj.msg" />

              <template v-if="obj.res">
                {
   {
    intp(obj.res.rec.set.bitRate, 3) }}kbps {
   {
    intp(obj.res.rec.set.sampleRate, 5) }}hz 编码{
   {
   
                  intp(obj.res.blob.size, 6)
                }}b [{
   {
    obj.res.rec.set.type }}]{
   {
    intp(obj.res.duration, 6) }}ms

                <button @click="recdown(obj.idx)">下载</button>
                <button @click="recplay(obj.idx)">播放</button>

                <span v-html="obj.playMsg"></span>
                <span v-if="obj.down">
                  <span style="color: red">{
   {
    obj.down }}</span>

                  没弹下载?试一下链接或复制文本<button @click="recdown64(obj.idx)">生成Base64文本</button>

                  <textarea v-if="obj.down64Val" v-model="obj.down64Val"></textarea>
                </span>
              </template>
            </div>
          </div>
        </div>
      </div>

      <div v-if="state.recOpenDialogShow" class="bootomWrap">
        <div style="display: flex; height: 100%; align-items: center">
          <div style="flex: 1"></div>
          <div style="width: 240px; background: #fff; padding: 15px 20px; border-radius: 10px">
            <div style="padding-bottom: 10px">录音功能需要麦克风权限,请允许;如果未看到任何请求,请点击忽略~</div>
            <div style="text-align: center"><a @click="waitDialogClick" style="color: #0b1">忽略</a></div>
          </div>
          <div style="flex: 1"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script  setup>
import Recorder from 'recorder-core'
// 需要使用到的音频格式编码引擎的js文件统统加载进来,这些引擎文件会比较大
import 'recorder-core/src/engine/mp3'
import 'recorder-core/src/engine/mp3-engine'
import 'recorder-core/src/extensions/waveview'
import {
    reactive, ref } from 'vue'
import {
    ElNotification, ElMessage } from 'element-plus'

const LogAudioPlayer = ref()
const state = reactive({
   
  Rec: Recorder,
  type: 'mp3',
  bitRate: 16,
  sampleRate: 1
使用vue-video-player实现实时视频播放,需要先安装vue-video-player插件并引入相关的组件和样式文件。具体步骤如下: 1. 安装vue-video-player插件: ``` npm install vue-video-player --save ``` 2. 在Vue3的入口文件main.js中引入vue-video-player插件和相关的样式文件: ``` import { createApp } from 'vue'; import App from './App.vue'; import VueVideoPlayer from 'vue-video-player'; import 'video.js/dist/video-js.css'; import 'vue-video-player/src/custom-theme.css'; const app = createApp(App); app.use(VueVideoPlayer); app.mount('#app'); ``` 3. 在Vue3的模板中,使用vue-video-player组件来加载视频文件,并设置autoplay和controls属性来自动播放和显示控制条: ``` <template> <div> <video-player ref="videoPlayer" autoplay controls></video-player> </div> </template> ``` 4. 在Vue3的组件中,使用mounted生命周期方法来获取video-player组件的引用,并添加事件监听器来实时监控视频播放: ``` <script> export default { mounted() { const videoPlayer = this.$refs.videoPlayer.$refs.video; videoPlayer.addEventListener('timeupdate', this.handleTimeUpdate); }, methods: { handleTimeUpdate() { const videoPlayer = this.$refs.videoPlayer.$refs.video; const currentTime = videoPlayer.currentTime; // 在这里可以处理视频播放时间的更新 } } } </script> ``` 5. 在handleTimeUpdate方法中,可以获取当前视频播放的时间,并进行处理。例如,可以将当前时间展示在页面上,或者根据时间来触发其他的事件。 需要注意的是,vue-video-player是基于video.js和Vue.js的开源插件,支持多种视频格式和播放器样式。使用vue-video-player可以方便地实现视频播放和控制,同时也提供了一些自定义的配置选项和事件回调函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值