首页 > 其他分享 >Vue中使用benz-amr-recorder插件实现播放amr音频文件以及在线url跨域问题解决

Vue中使用benz-amr-recorder插件实现播放amr音频文件以及在线url跨域问题解决

时间:2022-09-23 15:57:27浏览次数:80  
标签:插件 Vue 跨域 springframework 播放 org import amr

场景

需要做一个Android端和Web端的聊天室,Android端的录音音频文件为.amr格式,除了

通过后台server端转码之后,是否可以通过插件在前端直接播放amr的音频文件。

benz-amr-recorder

https://benzleung.github.io/benz-amr-recorder/

纯前端解码、播放、录音、编码 AMR 音频,无须服务器支持,基于 amr.js 和 RecorderJs。

在线演示demo

https://benzleung.github.io/benz-amr-recorder/demo.html

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、安装插件

npm install benz-amr-recorder

2、播放amr

var amr = new BenzAMRRecorder();
amr.initWithUrl('path/to/voice.amr').then(function() {
  amr.play();
});
amr.onEnded(function() {
  alert('播放完毕');
})

3、在Vue中使用

 

<el-button type="button" @click="playAmrData">播放amr录音数据</el-button>

  data() {
    return {
      amr: null,
    };
  },

点击调用的方法

    //播放amr音频文件
    playAmrData(){
      this.amr = new BenzAMRRecorder();
      this.amr.initWithUrl("http://ip:20201/profile/upload/2022/09/22/0dfd2965-0427-4cc8-9702-d4f7fdf9c047.amr")
      .then(()=>{
        this.amr.play();
      })
      .catch((e)=>{
        this.$message.error("播放失败");
      })
    },

注意:

上面的url是amr文件的网络url,这里是本地静态资源映射的地址。

4、跨域问题

但是在播放时提示跨域

 

 

 

看一下官方对于跨域问题的说明

 

 

所以需要后台服务解决跨域问题。这里后台静态资源映射使用的SpringBoot

SpringBoot中重写addCorsMapping解决跨域以及提示list them explicitly or consider using “allowedOriginPatterns“ in

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/127011172

添加配置类代码

package com.chrisf.config;
 
import com.chrisf.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 
@Configuration
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
public class ResourcesConfig implements WebMvcConfigurer {
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
    }
 
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 拦截所有的请求
                //.allowedOrigins("*")  // 可跨域的域名,可以为 *
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("*")   // 允许跨域的方法,可以单独配置
                .allowedHeaders("*");  // 允许跨域的请求头,可以单独配置
    }
}

 

标签:插件,Vue,跨域,springframework,播放,org,import,amr
From: https://www.cnblogs.com/badaoliumangqizhi/p/16722993.html

相关文章

  • vue3配置全局过滤器
    vue3配置全局过滤器需要在main.js中配置一下代码//vue3配置全局过滤器app.config.globalProperties.$filters={//formatTime过滤器的名称formatTime(value:s......
  • Vuex新一代状态管理 — Pinia
    最近在学习pinia,做了一些笔记,在这里分享一下:https://note.youdao.com/ynoteshare/index.html?id=3bedfc4d66825be097cee904fe311f56&type=note&_time=1663899586848......
  • Vue中的事件修饰符
    Vue中的事件修饰符:1、prevent:阻止默认事件(常用)2、stop:阻止事件冒泡(常用)3、once:事件只触发一次(常用);4、capture:使用事件的捕获模式;5、self:只有event.target是当前操作的......
  • vue收集表单数据给后端交互
    vue项目中需要收集表单数据给后端发送请求进行交互:查看代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compa......
  • PS插件:复古工笔画风PS插件,一键生成古风写真!
    工笔就是细致工整又写实。亦称“细笔”,相当于书法中的正楷书法。与”写意”对称。中国画技法名。属于工整细致一类密体的画法。用细致的笔法制作,工笔画着重线条美,一丝不苟......
  • vue3源码学习12-编译three-生成代码
    之前两节看了模板生成AST和AST内部转化,这一节看最后的生成代码,编译配置是mode为module,prefixIdentifiers开启,hoistStatic开启,其他配置均不开启,先看示例:源代码:<divclass=......
  • vue+echart+自定义指令:自适应图表
    vue+echart+自定义指令:自适应图表,图表根据宽高拉伸变化而重置变化。之前有用到过其它方式实现,现在只用指令来实现:<template><divclass="box"><divref="zhex"v-res......
  • Vue学习笔记
    模板语法条件渲染列表渲染//使用v-for指令<template><div><ul><liv-for="iteminnewsList":key="item.id">{{item.title}}</li></ul>......
  • 计算机毕业设计 SSM+Vue农家乐管理系统 农家院住宿管理系统 民宿旅游预约管理系统 Jav
    ......
  • vue记住账号密码(cookie)
    //安装npminstall--savejs-base64//引入constBase64=require('js-base64').Base64<template><formclass="main"><!--账号--><di......