最开始采用m4a格式的录音,文件能合并,但是无法正常播放
/**
* 生成验证码
* @param num
* @return
*/
public static String genCode(int num) {
StringBuffer sf = new StringBuffer();
Random random = new Random();
for (int i = 0; i < num; i++) {
int r = random.nextInt(9);
sf.append(r);
}
return sf.toString();
}
/**
* 获取绝对路径
* @param filepath
* @return
*/
public static String getRealPath(String filepath) {
String path = ClassLoader.getSystemResource("").getPath();
if (filepath.endsWith("/") || filepath.endsWith("\\")) {
return path + filepath;
}
return path + "/" + filepath;
}
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.apache.commons
commons-lang3
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servlet
javax.servlet-api
javax.servlet
jstl
taglibs
standard
1.1.2
org.springframework.boot
spring-boot-starter-test
test
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
获取语音验证码
/**
* @author sunyiran
* @date 2018-09-08
*/
@Controller
@RequestMapping("/code")
public class CodeController {
@GetMapping
public String getCode() throws IOException {
String code = CodeUtils.genCode(4);
CodeUtils.getCodeM4a(code);
return "index";
}
}
public static File getCodeM4a(String code) throws IOException {
File file = new File(getRealPath("/static/data/end.mp3"));
if (file.exists()) {
file.delete();
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
FileInputStream fis1 = new FileInputStream(getRealPath("/static/data/base.mp3"));
Vector v = new Vector<>();
v.add(fis1);
char[] chars = code.toCharArray();
for (char name : chars) {
FileInputStream fis = new FileInputStream(getRealPath("/static/data/"+name+".mp3"));
v.add(fis);
}
Enumeration en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
int b;
while((b = sis.read())!= -1)
{
fos.write(b);
}
sis.close();
fos.close();
fis1.close();
return file;
}