功能需求:redis
1、獲取本地音頻文件,進行解析成二進制數據音頻流spring
2、將音頻流轉化成byte[]數組,按指定大小字節數進行分包數據庫
3、將音頻流分紅若干個包,以List列表形式緩存到redis數據庫中數組
4、從redis數據庫中獲取數據,轉換成音頻流輸出到瀏覽器播放、實現音頻下載功能瀏覽器
程序以下:緩存
1 <!--Redis依賴--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-redis</artifactId> 5 </dependency>
1 # 服務端口 2 server: 3 port: 8080 4 5 spring: 6 #reids配置 7 redis: 8 host: 127.0.0.1 # Redis服務器地址 9 database: 1 # Redis數據庫索引(默認爲0) 10 port: 6379 # Redis服務器鏈接端口 11 password: # Redis服務器鏈接密碼(默認爲空) 12 jedis: 13 pool: 14 max-active: 8 # 鏈接池最大鏈接數(使用負值表示沒有限制) 15 max-wait: -1ms # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) 16 max-idle: 8 # 鏈接池中的最大空閒鏈接 17 min-idle: 0 # 鏈接池中的最小空閒鏈接 18 timeout: 3000ms # 鏈接超時時間(毫秒)
說的通俗一點…爲了讓Spring框架體系可以更加方便的接入Redis的功能,RedisTemplate其實就是Spring框架對Jedis的封裝…是 spring-data-redis中使用redis的模版。服務器
/** * 建立redisTemplate對象操做redis */ @Resource private RedisTemplate<String,Object> redisTemplate;
經過FileInputStream對象把音頻文件轉換成byte[]數組,進行分包,把分好包的字節數據添加到List集合中,在調用RedisTemplate對象的opsForList().rightPushAll方法批量添加參數List元素,以Redis的列表數據格式存儲。app
1 /** 2 * 獲取文件將文件轉換成byte[]數組,進行分包存儲到redis 3 */ 4 @RequestMapping("/setAudio") 5 @ResponseBody 6 public Object getsty() throws Exception { 7 File file = new File("E:/zmj-3011-32779/12121.mp3"); 8 FileInputStream inputFile = new FileInputStream(file); 9 byte[] buffer = new byte[(int) (file.length() * 1)]; 10 inputFile.read(buffer);//文件解析把字節數添加到buffer[]中 11 inputFile.close(); 12 13 int viceLength = 180; //每一個字節包大小 14 int viceNumber = (int) Math.ceil(buffer.length /(double) viceLength);//存多少個包 15 int from, to; 16 List listrk = new ArrayList(); 17 for (int i=0;i<viceNumber;i++){ //將完整音頻buffer[]進行循環拆分 18 ioentity ioe=new ioentity(); 19 from=(int) (i*viceLength); 20 to=(int)(from+viceLength); 21 if(to>buffer.length) 22 to=buffer.length; 23 listrk.add(Arrays.copyOfRange(buffer,from,to));//按字節範圍拷貝生成新數組,添加到List列表中 24 } 25 redisTemplate.opsForList().rightPushAll("Audio", listrk);//redisTemplate的批量添加,以List列表形式進行存儲 26 return "redis入庫成功!"; 27 }
redis客戶端存儲結果:框架
能夠看出只存儲了一個key,value是以list列表形式存儲,音頻文件以180個字節數組進行存儲,一共存儲了2634個。此處沒有設緩存時間,因此不會超時。函數
經過Redis對象的redisTemplate.opsForList().range方法獲取緩存的value,經過list集合接收進行遍歷,進行合併生成一個新的byte數組,在經過OutputStream對象輸出byte數組,瀏覽器自動解析二進制音頻流文件。
1 /** 2 * 從redis中分包取值進行byte[]數組合並解析音頻 3 */ 4 @RequestMapping("/getkeyAudio") 5 public Object getKey(HttpServletResponse response) throws Exception{ 6 OutputStream os = response.getOutputStream(); 7 List list =redisTemplate.opsForList().range("Audio", 0, -1); //經過key獲取指定區間的值,List方式存儲用List集合去接收 8 9 //合併音頻 10 List<byte[]> blist = list; 11 int lengthTotal = 0; 12 for (byte[] item : blist) { 13 lengthTotal += item.length; 14 } 15 byte[] totalByte = new byte[lengthTotal]; 16 int begin = 0; 17 for (byte[] item : blist) { 18 //System.arraycopy(原數組, 原數組起始位置, 目標數組, 目標數組起始位置, 複製長度); 19 System.arraycopy(item, 0, totalByte, begin, item.length); 20 begin += item.length; 21 } 22 os.write(totalByte);//經過OutputStream對象輸出合併後的數組 23 24 return ""; //OutputStream對象輸出流,直接返回爲空,瀏覽器自動會爲咱們解析音頻流 25 }
第一種解析方法:
瀏覽器發起請求獲得音頻二進制流,瀏覽器解析自動生成一個播放器播放該音頻及附加下載功能。
第二種解析方法:
在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>
記錄點滴天天成長一點點,學習是永無止境的!轉載請附原文連接!!!