請各大網友尊重本人原創知識分享,謹記本人博客:博客園、CSDN
功能需求:
一、獲取本地音頻檔案,進行決議成二進制資料音頻流
二、將音頻流轉化成byte[]陣列,按指定大小位元組數進行分包
三、將音頻流分成若干個包,以List串列形式快取到redis資料庫中
四、從redis資料庫中獲取資料,轉換成音頻流輸出到瀏覽器播放、實作音頻下載功能
程式如下:
1.在SpringBootpom.xml檔案中添加Redis依賴
1 <!--Redis依賴--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-redis</artifactId> 5 </dependency>
2.在SpringBoot組態檔中添加以下配置
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 # 連接超時時間(毫秒)
3.創建RedisTemplate物件操作redis
RedisTemplate介紹:
說的通俗一點…為了讓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的串列資料格式存盤,
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個,此處沒有設快取時間,所以不會超時,

6.從Redis資料庫快取中獲取音頻資料進行決議
通過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>

個人總結:
記錄點滴每天成長一點點,學習是永無止境的!轉載請附原文鏈接!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/137112.html
標籤:Java
下一篇:java_環境搭建、變數的使用
