功能需求:
一、獲取本地音頻文件,進行解析成二進制數據音頻流
二、將音頻流轉化成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 RedisTemplateString,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;iviceNumber;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集合去接收
//合并音頻
Listbyte[]> 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內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 解決Spring session(redis存儲方式)監聽導致創建大量redisMessageListenerContailner-X線程問題
- Redis如何存儲對象與集合示例詳解
- php+redis實現多臺服務器內網存儲session并讀取示例
- 詳解Asp.net Core 使用Redis存儲Session
- php實現Session存儲到Redis
- php Session存儲到Redis的方法