Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call system loudspeaker by Java

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to call the system loudspeaker by Java". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to call the system loudspeaker by Java" can help you solve the problem.

Mode of realization

Then we will analyze and implement this small function. Write a Demo first.

1. First of all, we need a dll as an aid. Here explain the meaning of dll (DLL (Dynamic Link Library) file is a dynamic link library file, also known as "application hundred program development", is a software file type. In Windows, many applications are not a complete executable file, they are divided into some known pairs of independent dynamic link libraries, namely DLL files, placed in the Tao system. When we execute a program, the corresponding version of the DLL file is called. An application can use multiple DLL files, and one DLL file may also be used by different applications. Such DLL files are called shared DLL files.

You need to copy the jacob-1.17-M2-x64.dll to the C:WindowsSystem32 directory. We can also see that there are a lot of .dll files in the directory.

The documents here are easy to find under your own Baidu.

two。 Import coordinates using the maven project.

Net.sf.jacob-project jacob 1.14.3

3. Test the class code.

/ * * text-to-voice test jdk bin file needs to be imported into jacob-1.17-M2-x64.dll * Note Guide package * @ date: 10:05:21 on February 25, 2020 * / public class Jacobtest {public static void main (String [] args) {textToSpeech ("attention, staff, table 8001 customers are looking for help!") ;} / * Voice to text and play * * @ param text * / public static void textToSpeech (String text) {ActiveXComponent ax = null; try {ax = new ActiveXComponent ("Sapi.SpVoice"); / / output voice content Dispatch spVoice = ax.getObject () / / Volume 0-100 ax.setProperty ("Volume", new Variant (100)); / / speech reading speed-10 to + 10 ax.setProperty ("Rate", new Variant (0)); / / perform Dispatch.call (spVoice, "Speak", new Variant (text)) / * / / the following is the build file stream to generate voice files ax = new ActiveXComponent ("Sapi.SpFileStream"); Dispatch spFileStream = ax.getObject (); ax = new ActiveXComponent ("Sapi.SpAudioFormat"); Dispatch spAudioFormat = ax.getObject () / / set the audio stream format Dispatch.put (spAudioFormat, "Type", new Variant (22)); / / set the file output stream format Dispatch.putRef (spFileStream, "Format", spAudioFormat) / / call the output file stream opening method to create a .wav file Dispatch.call (spFileStream, "Open", new Variant (". / text.wav"), new Variant (3), new Variant (true); / / set the audio output stream of the sound object to the output file object Dispatch.putRef (spVoice, "AudioOutputStream", spFileStream) / / set volume from 0 to 100 Dispatch.put (spVoice, "Volume", new Variant (100)); / / set reading speed Dispatch.put (spVoice, "Rate", new Variant (- 2)); / / start reading Dispatch.call (spVoice, "Speak", new Variant (text)) / / close the output file Dispatch.call (spFileStream, "Close"); Dispatch.putRef (spVoice, "AudioOutputStream", null); spAudioFormat.safeRelease (); spFileStream.safeRelease (); * / spVoice.safeRelease (); ax.safeRelease ();} catch (Exception e) {e.printStackTrace () }}}

4. As you can see from the test class, this method can both make sound and output files with the suffix .wav, which is a standard multimedia file. The above code comments are very clear, do not explain, see for yourself.

5. The test was successful and is now integrated into your own project.

Separately stated

When it comes to calling loudspeakers to make sound, you can also think about how to call microphone radio.

Public class EngineeCore {String filePath = "E:\ voice\ voice_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 int sampleSizeInBits = 16 22050 int sampleSizeInBits 44100 int sampleSizeInBits = 16; / 8 int channels 16 int channels = 1 / / 1 boolean signed = true; / / true,false boolean bigEndian = false; / / true,false return new AudioFormat (sampleRate, sampleSizeInBits, channels, signed, bigEndian);} / / end getAudioFormat private void startRecognize () {try {/ / get the specified audio format 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 / / the weight of sound input int weight = 2; / / the count to determine whether to stop or not 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) / / start storing bytes when the last bit of the array is greater than weight (sound is passed in). Once you start, you no longer need to judge the last bit if (Math.abs (fragment [fragment.length-1]) > weight | | baos.size () > 0) {baos.write (fragment) System.out.println ("Guard:" + fragment [0] + ", ending with" + fragment [fragment.length-1] + ", lenght" + fragment.length); / / determine whether the voice stops if (Math.abs (fragment.length-1]) 20) {System.out.println ("stop entry") Break;} / / get recording input stream audioFormat = getAudioFormat (); byte audioData [] = baos.toByteArray (); bais = new ByteArrayInputStream (audioData) Ais = new AudioInputStream (bais, audioFormat, audioData.length / audioFormat.getFrameSize ()); / / define the final saved file name System.out.println ("start generating voice file"); AudioSystem.write (ais, AudioFileFormat.Type.WAVE, audioFile); downSum = 0; stopRecognize () } catch (Exception e) {e.printStackTrace ();} finally {/ / close the stream try {ais.close (); bais.close (); baos.reset () } catch (IOException e) {e.printStackTrace ();} / / end run} / / end inner class CaptureThread on "how to call system speakers by Java" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 291

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report