其他分享
首页 > 其他分享> > 听书神器

听书神器

作者:互联网

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Tingshu {

public static void main(String[] args) throws UnsupportedFlavorException, IOException, InterruptedException {
// TODO Auto-generated method stub


Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//接收一个字符串
StringSelection selection = new StringSelection("123");
//获得剪切板内容
clipboard.setContents(selection, null);
while (true) {
//拿到剪切板内容

Transferable transferable = clipboard.getContents(null); //先定义一个字符串 得到一个转换数据 得到字符串
String search = (String)transferable.getTransferData(DataFlavor.stringFlavor);
//读取一个新文件
Scanner scanner = new Scanner(new File("D:/456.txt"));

while (scanner.hasNext()) {
String aaa = scanner.nextLine();

if (aaa.contains(search)) {

} ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
Dispatch sapo = sap.getObject();
try {

sap.setProperty("Volume", new Variant(100));
sap.setProperty("Rate", new Variant(5));

Variant defalutVoice = sap.getProperty("Voice");

Dispatch dispdefaultVoice = defalutVoice.toDispatch();
Variant allVoices = Dispatch.call(sapo, "GetVoices");
Dispatch dispVoices = allVoices.toDispatch();

Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);

Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
Dispatch.call(sapo, "Speak", new Variant(aaa));

} catch (Exception e) {
e.printStackTrace();
} finally {
sapo.safeRelease();
sap.safeRelease();
}
Thread.sleep(50);


}}}}

标签:听书,java,ActiveXComponent,Variant,Dispatch,神器,import,new
来源: https://www.cnblogs.com/p4567/p/15022262.html