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

微信公眾號(hào)錄音文件的播放與保存(amr文件轉(zhuǎn)mp3)

 更新時(shí)間:2021年08月12日 10:44:21   作者:llsydn  
本文主要介紹了微信公眾號(hào)錄音文件的播放與保存(amr文件轉(zhuǎn)mp3),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

音頻轉(zhuǎn)碼工具,主要用于將微信語(yǔ)音 amr 格式轉(zhuǎn)換為 mp3 格式以便在 html5 的 audio 標(biāo)簽中進(jìn)行播放

1.調(diào)用微信提供的接口獲取錄音的InputStream字節(jié)流

public InputStream getInputStream(String mediaId) {
    InputStream is = null;
    try {
        String URL_DOWNLOAD_TEMP_MEDIA = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
        String url = URL_DOWNLOAD_TEMP_MEDIA.replace("ACCESS_TOKEN", "自己寫(xiě)代碼獲取accessToken").replace("MEDIA_ID", mediaId);
        URL urlGet = new URL(url);
        HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
        http.setRequestMethod("GET"); // 必須是get方式請(qǐng)求
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setDoOutput(true);
        http.setDoInput(true);
        System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 連接超時(shí)30秒
        System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 讀取超時(shí)30秒
        http.connect();
        // 獲取文件轉(zhuǎn)化為byte流
        is = http.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return is;
}

2.將獲取到的字節(jié)流保存為amr文件

public String downloadMediaId(HttpServletRequest request, String mediaId) {
    String relfilePath = null;
    InputStream inputStream = getInputStream(mediaId);
    FileOutputStream fileOutputStream = null;
    try {
        //服務(wù)器資源保存路徑
        String savePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + DateUtil.getYear() + "/wxmedia/audio/";
        savePath = savePath + "audio/"; 
        String filename = String.valueOf(System.currentTimeMillis()) + ".amr";
        relfilePath = "upload/" + DateUtil.getYear() + "/wxmedia/audio/" + filename;
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        byte[] data = new byte[1024];
        int len = 0;
        fileOutputStream = new FileOutputStream(savePath + filename);
        while ((len = inputStream.read(data)) != -1) {
            // 判斷結(jié)果是否有錯(cuò)
            if (new String(data).indexOf("errmsg") > -1) {
                return null;
            }
            fileOutputStream.write(data, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return relfilePath;
}

3.將保存的amr文件轉(zhuǎn)成mp3文件

public void amrToMp3(String sourcePath, String targetPath) {
    File source = new File(sourcePath);
    File target = new File(targetPath);
    AudioUtils.amrToMp3(source, target);
}

4.所需的jar包依賴

<!--amr文件轉(zhuǎn)音頻map文件-->
<dependency>
    <groupId>com.github.dadiyang</groupId>
    <artifactId>jave</artifactId>
    <version>1.0.3</version>
</dependency>

音頻轉(zhuǎn)碼工具

支持 Linux/Windows/Mac 平臺(tái)
因?yàn)槭腔?JAVE 項(xiàng)目的修改,而 JAVE 是依賴 ffmpeg 所以可以適用于所有 ffmpeg 所支持的文件格式的轉(zhuǎn)換。具體可以查看 JAVE 官方文檔

原理

初始化時(shí)判斷當(dāng)前運(yùn)行環(huán)境,將bin目錄中對(duì)應(yīng)的 ffmpeg 可執(zhí)行文件拷貝到臨時(shí)目錄中
根據(jù)文件類型及配置通過(guò) Runtime.getRuntime().exec(cmd) 執(zhí)行 ffmpeg 對(duì)應(yīng)的轉(zhuǎn)碼命令

JAVE 項(xiàng)目的問(wèn)題

ffmpeg 是依賴運(yùn)行環(huán)境的,JAVE 項(xiàng)目封裝了ffmpeg,它通過(guò)上述的原理使 java 可以調(diào)用ffmpeg而且支持跨平臺(tái)。

  • 項(xiàng)目老舊沒(méi)再維護(hù)。官網(wǎng)最近版本是2009年發(fā)布的,其依賴的ffmpeg早已過(guò)時(shí),很多情況下用不了。
  • 轉(zhuǎn)碼一直報(bào)異常 EncoderException: Stream mapping
  • 沒(méi)有發(fā)布maven倉(cāng)庫(kù),而且 JAVE 本身也不是一個(gè)maven項(xiàng)目
  • 不支持mac

本項(xiàng)目特點(diǎn)

本項(xiàng)目為解決上述問(wèn)題而生。

  • 這是一個(gè)maven項(xiàng)目,而且已發(fā)布到中央倉(cāng)庫(kù)。
  • 項(xiàng)目依賴的 ffmpeg 可執(zhí)行文件經(jīng)過(guò)驗(yàn)證可以使用(單元測(cè)試中提供了一個(gè)簡(jiǎn)單的檢驗(yàn)方法)
  • 解決了amr轉(zhuǎn)mp3出現(xiàn)的 EncoderException: Stream mapping
  • 支持 Linux/Windows/Mac 平臺(tái)

擴(kuò)展

如果程序無(wú)法通過(guò)拷貝資源文件的方式獲取到 ffmpeg 的可執(zhí)行文件或者內(nèi)置的 ffmpeg 不支持你所使用的操作系統(tǒng)
你可以通過(guò)環(huán)境變量或者在 java 中設(shè)置 System.setProperty("ffmpeg.home", "ffmpeg可執(zhí)行文件所在的目錄") 的方式指定你的系統(tǒng)中安裝的可用的 ffmpeg 文件的目錄

如 System.setProperty("ffmpeg.home", "/usr/local/bin/")

到此這篇關(guān)于微信公眾號(hào)錄音文件的播放與保存(amr文件轉(zhuǎn)mp3)的文章就介紹到這了,更多相關(guān)微信公眾號(hào)錄音內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論