欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java音頻處理之音頻流轉(zhuǎn)音頻文件和獲取音頻播放時(shí)長詳解

 更新時(shí)間:2025年07月21日 10:22:56   作者:大魚>  
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)音頻流轉(zhuǎn)音頻文件和獲取音頻播放時(shí)長功能,文中的示例代碼簡潔易懂,有需要的小伙伴可以了解下

1.背景

最近對接了一款智能手表,手環(huán),可以應(yīng)用與老人與兒童監(jiān)控,環(huán)衛(wèi)工人監(jiān)控,農(nóng)場畜牧業(yè)監(jiān)控,寵物監(jiān)控等,其中用到了音頻傳輸,通過平臺(tái)下發(fā)語音包,發(fā)送遠(yuǎn)程命令錄制當(dāng)前設(shè)備音頻并將音頻分包傳輸?shù)椒?wù)器上生成音頻文件等。其中關(guān)于音頻的一些簡單操作封裝成了工具包。

2.音頻工具包

引入jaudiotagger,用來獲取MP3格式的音頻時(shí)長。

        <dependency>
            <groupId>org</groupId>
            <artifactId>jaudiotagger</artifactId>
            <version>2.0.1</version>
        </dependency>

工具包代碼:AudioUtils

package com.xxxx.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 音頻處理工具類
 * @author Mr.Li
 * @date 2023-10-26
 */
@Slf4j
public class AudioUtils {
    /**
     * 二進(jìn)制流轉(zhuǎn)音頻文件
     * @param binaryData
     * @param outputFilePath
     * @throws IOException
     */
    public static boolean convertBinaryToAudio(byte[] binaryData, String outputFilePath) throws IOException {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(outputFilePath);
            outputStream.write(binaryData);
            return true;
        }catch (Exception e){
            log.error("convertBinaryToAudio:outputFilePath:{}",outputFilePath,e);
            return false;
        }finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
    /**
     * 獲取AMR格式音頻長度
     * @param file
     * @return
     * @throws IOException
     */
    public static int getAmrDuration(File file) throws IOException {
        long duration = -1;
        int[] packedSize = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0,
                0, 0 };
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            // 文件的長度
            long length = file.length();
            // 設(shè)置初始位置
            int pos = 6;
            // 初始幀數(shù)
            int frameCount = 0;
            int packedPos = -1;
            // 初始數(shù)據(jù)值
            byte[] datas = new byte[1];
            while (pos <= length) {
                randomAccessFile.seek(pos);
                if (randomAccessFile.read(datas, 0, 1) != 1) {
                    duration = length > 0 ? ((length - 6) / 650) : 0;
                    break;
                }
                packedPos = (datas[0] >> 3) & 0x0F;
                pos += packedSize[packedPos] + 1;
                frameCount++;
            }
            // 幀數(shù)*20
            duration += frameCount * 20;
        } catch (Exception e){
            log.error("getAmrDuration:",e);
        }finally {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
        }
        return (int)((duration/1000)+1);
    }

    /**
     * 計(jì)算Mp3音頻格式時(shí)長
     * @param mp3File
     * @return
     */
    public static int getMp3Duration(File mp3File) {
        try {
            MP3File f = (MP3File) AudioFileIO.read(mp3File);
            MP3AudioHeader audioHeader = (MP3AudioHeader) f.getAudioHeader();
            return audioHeader.getTrackLength();
        } catch (Exception e) {
            log.error("getMp3Duration:",e);
            return 0;
        }
    }

    public static void main(String[] args) throws IOException {
        String path="C:\\Users\\MyPC\\Desktop\\卡布奇諾-王逗逗.mp3";
        int duration = getMp3Duration(new File(path));
        System.out.println(duration);
    }
}

致力于物聯(lián)網(wǎng)應(yīng)用開發(fā),目前有一套成熟的物聯(lián)網(wǎng)底層服務(wù)與物聯(lián)網(wǎng)設(shè)備管理系統(tǒng),并提供API,WebHook,MQTT實(shí)現(xiàn)將數(shù)據(jù)實(shí)時(shí)有效的推送到客戶的云平臺(tái),助力客戶完成自己的SaaS平臺(tái)開發(fā)。

3.知識(shí)延展

java 獲取MP3文件播放時(shí)長

方法一:

代碼簡單:

    public static int getMp3TrackLength(File mp3File) {
        try {
            MP3File f = (MP3File) AudioFileIO.read(mp3File);
            MP3AudioHeader audioHeader = (MP3AudioHeader)f.getAudioHeader();
            return audioHeader.getTrackLength();
        } catch(Exception e) {
            return -1;
        }
    }

maven 依賴包:

        <dependency>
            <groupId>org</groupId>
            <artifactId>jaudiotagger</artifactId>
            <version>2.0.1</version>
        </dependency>

方法二:

獲取網(wǎng)絡(luò)資源音頻時(shí)長:

這種方法是獲取文件字節(jié)大小然后在用公式自己算的

BufferedInputStream bis = null;
        Bitstream bitstream = null;
        try {
            URL url = new URL("http://*********08.mp3");
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.connect();
            //此url響應(yīng)頭中包含"Accept-Length"為字節(jié)數(shù)大小
            String headerField = urlConn.getHeaderField("Accept-Length");
            bis = new BufferedInputStream(urlConn.getInputStream());

            bitstream = new Bitstream(bis);
            Header header = bitstream.readFrame();
            int bitrate = header.bitrate();
            //根據(jù)文件大小計(jì)算 字節(jié)數(shù)*8/碼率/1000(毫秒值轉(zhuǎn)秒)
            int timeLong = Integer.parseInt(headerField)*8/bitrate;
            System.out.println(timeLong);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bitstream!=null){
                try {
                    bitstream.close();
                } catch (BitstreamException e) {
                    e.printStackTrace();
                }
            }
        }

BitStream的包在這里,maven引入

<dependency>
            <groupId>com.badlogicgames.jlayer</groupId>
            <artifactId>jlayer</artifactId>
            <version>1.0.2-gdx</version>
        </dependency>

方法三:

根據(jù)content length獲取網(wǎng)絡(luò)音頻時(shí)長,有誤差:

public static void main(String[] args) {
        try {
            long startTime=System.currentTimeMillis();   //獲取開始時(shí)間
            URL urlfile = new URL("http://resource.puxinwangxiao.com/b4ef18fe62948ab2528127c8c1357ddd.mp3");
            //File file = new File("C:\\music\\test2.mp3");
            //URL urlfile = file.toURI().toURL();
            URLConnection con = urlfile.openConnection();
            int b = con.getContentLength();// 得到音樂文件的總長度
            BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
            Bitstream bt = new Bitstream(bis);
            Header h = bt.readFrame();
            int time = (int) h.total_ms(b);
            System.out.println(time / 1000);
            long endTime1=System.currentTimeMillis(); //獲取結(jié)束時(shí)間
            System.out.println("所需時(shí)間: "+(endTime1-startTime)+"ms");
        }catch (Exception e ){
            System.out.println(e.getMessage());
        }
    }

到此這篇關(guān)于Java音頻處理之音頻流轉(zhuǎn)音頻文件和獲取音頻播放時(shí)長詳解的文章就介紹到這了,更多相關(guān)Java音頻處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決@FeignClient注入service失敗問題

    解決@FeignClient注入service失敗問題

    這篇文章主要介紹了解決@FeignClient注入service失敗問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 關(guān)于Controller 層返回值的公共包裝類的問題

    關(guān)于Controller 層返回值的公共包裝類的問題

    本文給大家介紹Controller 層返回值的公共包裝類-避免每次都包裝一次返回-InitializingBean增強(qiáng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-09-09
  • 使用springboot配置和占位符獲取配置文件中的值

    使用springboot配置和占位符獲取配置文件中的值

    這篇文章主要介紹了使用springboot配置和占位符獲取配置文件中的值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java中的邏輯控制語句詳解

    Java中的邏輯控制語句詳解

    下面小編就為大家?guī)硪黄狫ava邏輯控制的基礎(chǔ)文章。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-08-08
  • Springboot怎么解決循環(huán)依賴(基于單例 Bean)

    Springboot怎么解決循環(huán)依賴(基于單例 Bean)

    循環(huán)依賴指兩個(gè)或多個(gè) Bean 之間相互依賴,導(dǎo)致在創(chuàng)建 Bean 的過程中出現(xiàn)“死循環(huán)”,增加了對象依賴的混亂性,依賴關(guān)系變得錯(cuò)綜復(fù)雜,下面給大家介紹Springboot怎么解決循環(huán)依賴,感興趣的朋友一起看看吧
    2025-05-05
  • MyBatis 延遲加載、一級緩存、二級緩存(詳解)

    MyBatis 延遲加載、一級緩存、二級緩存(詳解)

    下面小編就為大家?guī)硪黄狹yBatis 延遲加載、一級緩存、二級緩存(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • IDEA 2019.1.3 激活碼大全

    IDEA 2019.1.3 激活碼大全

    本文是小編給大家收藏整理的IDEA 2019.1.3 激活碼大全,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-10-10
  • Java基礎(chǔ)類Class使用指南

    Java基礎(chǔ)類Class使用指南

    關(guān)于通過類名訪問class屬性,我朋友問過好幾次了,一直沒明白這個(gè)東西到底是什么?對此,我參照網(wǎng)友們的博客,總結(jié)了一些小知識(shí),如發(fā)現(xiàn)錯(cuò)誤,希望糾正,謝謝
    2015-12-12
  • 簡單談?wù)凧ava中的方法和方法重載

    簡單談?wù)凧ava中的方法和方法重載

    下面小編就為大家?guī)硪黄唵握務(wù)凧ava中的方法和方法重載。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • java刪除文件夾的實(shí)現(xiàn)方式

    java刪除文件夾的實(shí)現(xiàn)方式

    用戶發(fā)現(xiàn)File.delete()無法刪除非空目錄,嘗試自定義遞歸刪除方法后,發(fā)現(xiàn)commons-io已提供FileUtils.deleteDirectory,建議優(yōu)先使用第三方庫避免重復(fù)造輪子,提升效率與代碼可靠性
    2025-07-07

最新評論