当前位置: 移动技术网 > IT编程>数据库>Redis > 将音频文件转二进制分包存储到Redis的实现方法(奇淫技巧操作)

将音频文件转二进制分包存储到Redis的实现方法(奇淫技巧操作)

2020年07月25日  | 移动技术网IT编程  | 我要评论

功能需求:

一、获取本地音频文件,进行解析成二进制数据音频流

二、将音频流转化成byte[]数组,按指定大小字节数进行分包

三、将音频流分成若干个包,以list列表形式缓存到redis数据库中

四、从redis数据库中获取数据,转换成音频流输出到浏览器播放、实现音频下载功能

程序如下:

1.在springbootpom.xml文件中添加redis依赖

<!--redis依赖-->
 <dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-data-redis</artifactid>
 </dependency>

2.在springboot配置文件中添加以下配置

# 服务端口
server:
 port: 8080

spring:
#reids配置
redis:
 host: 127.0.0.1 # redis服务器地址
 database: 1 # redis数据库索引(默认为0)
 port: 6379 # redis服务器连接端口
 password: # redis服务器连接密码(默认为空)
 jedis:
 pool:
 max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
 max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
 max-idle: 8 # 连接池中的最大空闲连接
 min-idle: 0 # 连接池中的最小空闲连接
 timeout: 3000ms # 连接超时时间(毫秒)

3.创建redistemplate对象操作redisredistemplate介绍:

说的通俗一点…为了让spring框架体系能够更加方便的接入redis的功能,redistemplate其实就是spring框架对jedis的封装…是 spring-data-redis中使用redis的模版。

/**
 * 创建redistemplate对象操作redis
 */

 @resource
 private redistemplate<string,object> redistemplate;

4.主业务数据处理读取音频文件进行转换存储

通过fileinputstream对象把音频文件转换成byte[]数组,进行分包,把分好包的字节数据添加到list集合中,在调用redistemplate对象的opsforlist().rightpushall方法批量添加参数list元素,以redis的列表数据格式存储。

/**
 * 获取文件将文件转换成byte[]数组,进行分包存储到redis
 */
 @requestmapping("/setaudio")
 @responsebody
 public object getsty() throws exception {
 file file = new file("e:/zmj-3011-32779/12121.mp3");
 fileinputstream inputfile = new fileinputstream(file);
 byte[] buffer = new byte[(int) (file.length() * 1)];
 inputfile.read(buffer);//文件解析把字节数添加到buffer[]中
 inputfile.close();

 int vicelength = 180; //每个字节包大小
 int vicenumber = (int) math.ceil(buffer.length /(double) vicelength);//存多少个包
 int from, to;
 list listrk = new arraylist();
 for (int i=0;i<vicenumber;i++){ //将完整音频buffer[]进行循环拆分
  ioentity ioe=new ioentity();
  from=(int) (i*vicelength);
  to=(int)(from+vicelength);
  if(to>buffer.length)
  to=buffer.length;
  listrk.add(arrays.copyofrange(buffer,from,to));//按字节范围拷贝生成新数组,添加到list列表中
 }
 redistemplate.opsforlist().rightpushall("audio", listrk);//redistemplate的批量添加,以list列表形式进行存储
 return "redis入库成功!";
 }

redis客户端存储结果:

可以看出只存储了一个key,value是以list列表形式存储,音频文件以180个字节数组进行存储,一共存储了2634个。此处没有设缓存时间,所以不会超时。

6.从redis数据库缓存中获取音频数据进行解析

通过redis对象的redistemplate.opsforlist().range方法获取缓存的value,通过list集合接收进行遍历,进行合并生成一个新的byte数组,在通过outputstream对象输出byte数组,浏览器自动解析二进制音频流文件。

/**
 * 从redis中分包取值进行byte[]数组合并解析音频
 */
 @requestmapping("/getkeyaudio")
 public object getkey(httpservletresponse response) throws exception{
 outputstream os = response.getoutputstream();
 list list =redistemplate.opsforlist().range("audio", 0, -1); //通过key获取指定区间的值,list方式存储用list集合去接收

 //合并音频
 list<byte[]> blist = list;
 int lengthtotal = 0;
 for (byte[] item : blist) {
  lengthtotal += item.length;
 }
 byte[] totalbyte = new byte[lengthtotal];
 int begin = 0;
 for (byte[] item : blist) {
  //system.arraycopy(原数组, 原数组起始位置, 目标数组, 目标数组起始位置, 复制长度);
  system.arraycopy(item, 0, totalbyte, begin, item.length);
  begin += item.length;
 }
 os.write(totalbyte);//通过outputstream对象输出合并后的数组

 return ""; //outputstream对象输出流,直接返回为空,浏览器自动会为我们解析音频流
 }

第一种解析方法:

浏览器发起请求得到音频二进制流,浏览器解析自动生成一个播放器播放该音频及附加下载功能。

第二种解析方法:

在html页面中定义audio标签,创建xmlhttprequest对象发起请求,通过audio标签进行解析。

<audio id="sound" width="200" controls="controls"></audio>

<script>
 $(document).ready(function(){
 agf();
 });

 function agf() {
 //创建xmlhttprequest对象
 var xhr = new xmlhttprequest();
 //配置请求方式、请求地址以及是否同步
 xhr.open('post', '/getkey', true);
 xhr.setrequestheader("content-type","application/x-www-form-urlencoded");
 //设置请求结果类型为blob
 xhr.responsetype = 'blob';
 //请求成功回调函数
 xhr.onload = function(e) {
 if (this.status == 200) {//请求成功
  //获取blob对象
  var blob = this.response;
  //获取blob对象地址,并把值赋给容器
  $("#sound").attr("src", url.createobjecturl(blob));
 }
 };
 xhr.send(); 
 }
</script>

总结:

到此这篇关于将音频文件转二进制分包存储到redis的实现方法(奇淫技巧操作)的文章就介绍到这了,更多相关音频文件转二进制分包存储到redis内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网