java 調(diào)用本地?fù)P聲器的步驟
接下來就對這個(gè)小功能進(jìn)行分析和實(shí)現(xiàn)。先寫一個(gè)Demo。
1.首先,我們需要一個(gè)dll作為輔助。這里解釋一下dll的含義(DLL(Dynamic Link Library)文件為動(dòng)態(tài)鏈接庫文件,又稱“應(yīng)用百程序拓展”,是軟件文件類型。在Windows中,許多應(yīng)用程序并不是一個(gè)度完整的可執(zhí)行文件,它們被分割成一些相知對獨(dú)立的動(dòng)態(tài)鏈接庫,即DLL文件,放置于道系統(tǒng)中。當(dāng)我們執(zhí)行某一個(gè)程序時(shí),相應(yīng)的版DLL文件就會被調(diào)用。一個(gè)應(yīng)用程序可使用權(quán)多個(gè)DLL文件,一個(gè)DLL文件也可能被不同的應(yīng)用程序使用,這樣的DLL文件被稱為共享DLL文件)。
需要把jacob-1.17-M2-x64.dll復(fù)制到C:WindowsSystem32目錄下。我們也能看到目錄下有很多的.dll文件。
這里的文件大家自己百度下,很好找的。
2.使用maven項(xiàng)目導(dǎo)入坐標(biāo)。
<!-- https://mvnrepository.com/artifact/net.sf.jacob-project/jacob --><dependency><groupId>net.sf.jacob-project</groupId><artifactId>jacob</artifactId><version>1.14.3</version></dependency>
3.測試類代碼。
/** * 文字轉(zhuǎn)語音測試 jdk bin文件中需要導(dǎo)入jacob-1.17-M2-x64.dll * 注意導(dǎo)包哈 * @date: 2020年2月25日 上午10:05:21 */public class Jacobtest { public static void main(String[] args) {textToSpeech('工作人員請注意,桌號8001顧客正在尋求幫助??!'); } /** * 語音轉(zhuǎn)文字并播放 * * @param text */ public static void textToSpeech(String text) {ActiveXComponent ax = null;try { ax = new ActiveXComponent('Sapi.SpVoice'); // 運(yùn)行時(shí)輸出語音內(nèi)容 Dispatch spVoice = ax.getObject(); // 音量 0-100 ax.setProperty('Volume', new Variant(100)); // 語音朗讀速度 -10 到 +10 ax.setProperty('Rate', new Variant(0)); // 執(zhí)行朗讀 Dispatch.call(spVoice, 'Speak', new Variant(text)); /* // 下面是構(gòu)建文件流把生成語音文件 ax = new ActiveXComponent('Sapi.SpFileStream'); Dispatch spFileStream = ax.getObject(); ax = new ActiveXComponent('Sapi.SpAudioFormat'); Dispatch spAudioFormat = ax.getObject(); // 設(shè)置音頻流格式 Dispatch.put(spAudioFormat, 'Type', new Variant(22)); // 設(shè)置文件輸出流格式 Dispatch.putRef(spFileStream, 'Format', spAudioFormat); // 調(diào)用輸出 文件流打開方法,創(chuàng)建一個(gè).wav文件 Dispatch.call(spFileStream, 'Open', new Variant('./text.wav'), new Variant(3), new Variant(true)); // 設(shè)置聲音對象的音頻輸出流為輸出文件對象 Dispatch.putRef(spVoice, 'AudioOutputStream', spFileStream); // 設(shè)置音量 0到100 Dispatch.put(spVoice, 'Volume', new Variant(100)); // 設(shè)置朗讀速度 Dispatch.put(spVoice, 'Rate', new Variant(-2)); // 開始朗讀 Dispatch.call(spVoice, 'Speak', new Variant(text)); // 關(guān)閉輸出文件 Dispatch.call(spFileStream, 'Close'); Dispatch.putRef(spVoice, 'AudioOutputStream', null); spAudioFormat.safeRelease(); spFileStream.safeRelease();*/ spVoice.safeRelease(); ax.safeRelease();} catch (Exception e) { e.printStackTrace();} }}
4.從測試類可以看出,這個(gè)方法既可以發(fā)聲還能輸出后綴為.wav的文件,這是一個(gè)標(biāo)準(zhǔn)的多媒體文件。上述代碼注釋很清晰,就不解釋了,自己看哈。
5.測試成功,現(xiàn)在集成到自己的項(xiàng)目中。
另述這里說到了調(diào)用揚(yáng)聲器發(fā)聲,不放還可以想一下如何調(diào)用麥克風(fēng)收音。
public class EngineeCore { String filePath = 'E:voicevoice_cache.wav'; AudioFormat audioFormat; TargetDataLine targetDataLine; boolean flag = true; private void stopRecognize() {flag = false;targetDataLine.stop();targetDataLine.close(); } private AudioFormat getAudioFormat() {float sampleRate = 16000;// 8000,11025,16000,22050,44100int sampleSizeInBits = 16;// 8,16int channels = 1;// 1,2boolean signed = true;// true,falseboolean bigEndian = false;// true,falsereturn new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); }// end getAudioFormat private void startRecognize() {try { // 獲得指定的音頻格式 audioFormat = getAudioFormat(); DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo); // Create a thread to capture the microphone // data into an audio file and start the // thread running. It will run until the // Stop button is clicked. This method // will return after starting the thread. flag = true; new CaptureThread().start();} catch (Exception e) { e.printStackTrace();} // end catch }// end captureAudio method class CaptureThread extends Thread {public void run() { AudioFileFormat.Type fileType = null; File audioFile = new File(filePath); fileType = AudioFileFormat.Type.WAVE; //聲音錄入的權(quán)值 int weight = 2; //判斷是否停止的計(jì)數(shù) int downSum = 0; ByteArrayInputStream bais = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); AudioInputStream ais = null; try {targetDataLine.open(audioFormat);targetDataLine.start();byte[] fragment = new byte[1024];ais = new AudioInputStream(targetDataLine);while (flag) { targetDataLine.read(fragment, 0, fragment.length); //當(dāng)數(shù)組末位大于weight時(shí)開始存儲字節(jié)(有聲音傳入),一旦開始不再需要判斷末位 if (Math.abs(fragment[fragment.length-1]) > weight || baos.size() > 0) {baos.write(fragment);System.out.println('守衛(wèi):'+fragment[0]+',末尾:'+fragment[fragment.length-1]+',lenght'+fragment.length);//判斷語音是否停止if(Math.abs(fragment[fragment.length-1])<=weight){ downSum++;}else{ System.out.println('重置奇數(shù)'); downSum=0;} //計(jì)數(shù)超過20說明此段時(shí)間沒有聲音傳入(值也可更改)if(downSum>20){ System.out.println('停止錄入'); break;} }}//取得錄音輸入流audioFormat = getAudioFormat();byte audioData[] = baos.toByteArray();bais = new ByteArrayInputStream(audioData);ais = new AudioInputStream(bais, audioFormat, audioData.length / audioFormat.getFrameSize());//定義最終保存的文件名System.out.println('開始生成語音文件');AudioSystem.write(ais, AudioFileFormat.Type.WAVE, audioFile);downSum = 0;stopRecognize(); } catch (Exception e) {e.printStackTrace(); } finally {//關(guān)閉流try { ais.close(); bais.close(); baos.reset();} catch (IOException e) { e.printStackTrace();} }}// end run }// end inner class CaptureThread
這個(gè)測試沒測試,偷個(gè)懶找的“哈哈”。
還有一點(diǎn)是Java操作語音文件.wav先不要研究了 :laugh and cry:,這里涉及到了語音識別,但是有百度那么些api,有興趣的試試吧!
好了,在這里就結(jié)束了
更新博主把自己的畢設(shè)項(xiàng)目打包放到自己的服務(wù)器上,這個(gè)揚(yáng)聲器出現(xiàn)了新的問題。
本來所有的基礎(chǔ)都是在本地運(yùn)行的,通過調(diào)用本地dll文件實(shí)現(xiàn)揚(yáng)聲器發(fā)聲,現(xiàn)在部署到centOS上將會失去這個(gè)dll的支持,目前所存在的問題是如何不使用dll文件實(shí)現(xiàn)這個(gè)功能,中間借助了.wav后綴的音視頻文件。
如何在Linux上生成.wav的文件。如何獲取這個(gè)文件并輸出。(解釋一下,用餐顧客點(diǎn)擊手機(jī)網(wǎng)頁的菜單,然后再餐廳的主機(jī)來播放這個(gè)聲音)如何在輸出主機(jī)不進(jìn)行任何操作就能播放這個(gè)聲音或者能夠恢復(fù)之前的工作狀態(tài)。
現(xiàn)在的臨時(shí)解決辦法是本地跑一個(gè)呼叫服務(wù)的接口,當(dāng)需要這個(gè)功能的時(shí)候遠(yuǎn)程服務(wù)器調(diào)用本地跑的接口,進(jìn)而實(shí)現(xiàn)餐廳主機(jī)發(fā)聲。
這個(gè)和上面描述的并無差別,不一樣的是存在了兩臺主機(jī)的調(diào)用(當(dāng)然兩臺主機(jī)都應(yīng)該鏈接網(wǎng)絡(luò),能夠互相通信)
先寫到這了,當(dāng)有解決辦法的時(shí)候再更新吧!
以上就是java 調(diào)用本地?fù)P聲器的步驟的詳細(xì)內(nèi)容,更多關(guān)于java 調(diào)用本地?fù)P聲器的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析2. 一個(gè) 2 年 Android 開發(fā)者的 18 條忠告3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. Android studio 解決logcat無過濾工具欄的操作7. 什么是Python變量作用域8. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼9. Spring的異常重試框架Spring Retry簡單配置操作10. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼
