【Java音频操作】调用有道词典语音接口,生成单词MP3文件,支持自定义重 ...

【Java⾳频操作】调⽤有道词典语⾳接⼝,⽣成单词MP3⽂件,⽀持⾃定义重
复次数
参考博客:
运⾏效果
根据想要拼接的单词,⽣成⼀个单词朗读的mp3⽂件,可以⾃定义每个单词朗读时的重复次数。
代码思路:
先把要拼接的单词放进⼀个String数组中
然后遍历这个数组,⽤(伪)爬⾍下载每⼀个单词的⾳频⽂件
最后再把这些⾳频⽂件拼接起来,⽣成mp3⽂件 ↓
关于:有道词典语⾳接⼝
基于有道翻译的公共api可以制作⾃⼰的翻译app,前⼏天有这个想法,发现没有办法实现朗读功能,搜索了⼀下到了这个接⼝,解决了这个问题。⾮常简单,直接取⽹络地址进⾏播放就可以了,相当于播放⼀个⾳频⽂件,解析速度挺快。
美⾳:udao/dictvoice?type=0&audio=hello
英⾳:udao/dictvoice?type=1&audio=hello
api仅有两个参数,就是发⾳类型和单词,在audio=后⾯加上单词就ok了,type=0为美国发⾳,type=1为英国发⾳,做个程序⾃⼰⽤,⼏乎还可以吧。下⾯附上⼀个"hello"的测试,看下效果吧,第⼀个为美⾳,第⼆个为英⾳
关于:java播放mp3格式⾳频⽂件
package com.lt.music;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.*;
/***
- ⾳乐播放器类
- @author lt
- time 2016-7-5
*/
public class AudioPlayer{
Player player;
File music;
//构造⽅法参数是⼀个.mp3⾳频⽂件
public AudioPlayer(File file){
this.music = file;
}
//播放⽅法
public void play()throws FileNotFoundException, JavaLayerException {
BufferedInputStream buffer =new BufferedInputStream(new FileInputStream(music));
player =new Player(buffer);
player.play();
}
}
关于:从四级听⼒⽹站上下载MP3的代码(参考使⽤)
主程序类
package top.chen.dogwood;
import java.io.IOException;
import java.MalformedURLException;
/**
* 爬取指定链接的⼀组MP3 ⽂件
*
* 放⼊指定的⽬录中
*
* @author Geek
*
*/
public class Application {
public static void main(String[] args)throws MalformedURLException,
IOException {
String base ="download.dogwood/online/4jlxbx/";
String[] strings =new String[35];
for(int i =1; i <=35; i++){
strings[i-1]= base+String.format("%02d", i)+".mp3";
}
DownloadUtils downloadUtils  =new DownloadUtils(strings,"mp3","E:\\360Downloads\\TempFile"); try{
mp3制作downloadUtils.httpDownload();
}catch(Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下载⼯具类
package top.chen.dogwood;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.HttpURLConnection;
import java.URL;
public class DownloadUtils {
// ⽬标链接字符串
private String[] urlString;
// ⽬标⽂件的格式
private String targetType;
// 存放⽂件路径
private File rootDir;
public String[]getUrlString(){
return urlString;
}
public void setUrlString(String[] urlString){
this.urlString = urlString;
}
public String getTargetType(){
return targetType;
}
public void setTargetType(String targetType){
this.targetType = targetType;
}
public File getRootDir(){
return rootDir;
}
public void setRootDir(File rootDir){
}
public DownloadUtils(String[] urlString, String targetType, File rootDir){ super();
this.urlString = urlString;
this.targetType = targetType;
}
public DownloadUtils(String[] urlString, String targetType, String rootDir){ super();
this.urlString = urlString;
this.targetType = targetType;
}
public DownloadUtils(){
super();
}
/**
* 开始下载
*
* @throws Exception
*/
public void httpDownload()throws Exception {
public void httpDownload()throws Exception {
validate();
final String[] urls = urlString;
HttpURLConnection urlConnection;
for(int i =0; i < urls.length; i++){
urlConnection =(HttpURLConnection)new URL(urls[i])
.openConnection();
// 开启链接
InputStream inputStream = InputStream();
File temp =new File(rootDir,
String.format("%02d",i+1)+"."+targetType);
if(!ists()){
}
FileOutputStream fileOutputStream =new FileOutputStream(temp,true); int tem;
while(-1!=(tem = ad())){
fileOutputStream.write(tem);
fileOutputStream.flush();
}
fileOutputStream.close();
inputStream.close();
}
}
private void validate()throws Exception {
if(urlString.length <=0){
throw new Exception("下载路径不能为空!");
}
if(null == rootDir ||!ists()||!rootDir.isDirectory()){
throw new Exception("⽬标⽂件夹不存在!");
}
}
}
不过单线程下载速度有点慢 ,以后有空考虑下改成多线程下载
关于:⽂件的拼接(未使⽤,仅参考)
import java.io.*;
public class D3 {
public static void main(String[] args)throws Exception {
BufferedOutputStream buff=new BufferedOutputStream(new FileOutputStream("D:\\zuoye/text.mp3"));
FileInputStream int1=new FileInputStream("D:\\zuoye/太⽥美知彦 - 万⾥の长城.mp3");
FileInputStream int2=new FileInputStream("D:\\zuoye/阿桑 - 寂寞在唱歌.mp3");
//FileInputStream int3=new FileInputStream("D:\\zuoye/r.mp3");
BufferedInputStream but1=new BufferedInputStream(int1);
BufferedInputStream but2=new BufferedInputStream(int2);
//BufferedInputStream but3=new BufferedInputStream(int3);
SequenceInputStream seq1=new SequenceInputStream(but1, but2);//将两⾸歌合在⼀起
//SequenceInputStream seq2=new SequenceInputStream(seq1, but3);//将三⾸歌合在⼀起
int i;
while((ad())!=-1){
buff.write(i);
}
buff.close();
seq1.close();
//seq1.close();
but2.close();
}
}
本程序完整代码(下载+拼接MP3)
GLOBAL.java
package cn.hanquan.music;
import java.io.File;
public class GLOBAL {
/*-----------------------------------⾃定义begin-----------------------------------*/
// 填写单词
public static String sentence =new String(
"furnish establish qualification apply terrific thrill allegiance freelance objection substantial interpret fr
action denote fruitful inlet lame pinch remnant project or torrent incident zeal overlook shear propaganda prescribe cape");
//public static String sentence = new String("apply furnish establish qualification apply terrific");
public static String[] words = sentence.split(" ");// 分隔符号
// 存放路径
public static File rootDir =new File("C:\\mywords");// 存放⽂件路径
// 接⼝来源
public static String baseAPI=new String("udao/dictvoice?type=0&audio=");//t=1英⾳ t=0美英⾳ (建议t=1)
// 组合
public static boolean BIND =true;// 是否组合
public static String BINDNAME ="wordlist1";// 组合⽂件名
public static int REPEAT =2;// 组合重复次数
/*-----------------------------------⾃定义end-----------------------------------*/
// ⽂件格式(勿动)
public static String targetType ="mp3";// ⽬标⽂件的格式
// 完整⾳频url(勿动)
public static String[] wordsUrl =new String[GLOBAL.words.length];// ⽬标链接字符串
}
MusicPlay.java

本文发布于:2024-09-21 22:42:37,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/350662.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:单词   下载   朗读   拼接
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议