package com.fgkj.controller.media;
|
|
import com.fgkj.dto.ServiceModel;
|
import com.fgkj.dto.User_inf;
|
import com.fgkj.util.ActionUtil;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.Date;
|
|
@RequestMapping("broadcast")
|
@RestController
|
@Api(tags = "broadcast接口")
|
public class BroadcastController{
|
private static int createCount = 0; //生成音频文件计数
|
|
@RequestMapping("create")
|
@ApiOperation(notes = "",value="生成音频文件")
|
public ServiceModel createBroadcast(@RequestBody MyMultiMedia media){
|
MSTTSSpeech speech=new MSTTSSpeech();
|
// MyMultiMedia media = ActionUtil.getGson().fromJson(json, MyMultiMedia.class);
|
User_inf uinf = (User_inf)ActionUtil.getUser();
|
media.setMedia_name(getMediaName(uinf.getuName(),MSTTSSpeech.MYMEDIA_WAV));
|
media.setMedia_path(getRootFilePath(media.getMedia_name()));
|
|
boolean flag =speech.saveToWav(media.getMedia_text(), media.getMedia_path());
|
createCount++;
|
if(createCount % 50 == 0){
|
//清空之前生成的语音文件
|
|
if(createCount > 100000000){
|
createCount = 1;
|
}
|
}
|
ServiceModel model = new ServiceModel();
|
if(flag){
|
model.setCode(1);
|
model.setData(media);
|
}else{
|
model.setCode(0);
|
model.setData(media);
|
}
|
return model;
|
}
|
|
/**
|
* 根据当前用户名获取对应的文件名称
|
* @param uname
|
* @param fileType
|
* @return
|
*/
|
public String getMediaName(String uname,String fileType){
|
return (uname!=null?uname:"")+"_"+(new Date().getTime())+fileType;
|
}
|
|
public String getRootFilePath(String fileName){
|
String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
|
String str = new File(loadpath).getParentFile().getAbsolutePath();
|
String root = str+"/mediafiles/"; // 上传路径
|
String filePath = root+"/"+fileName;
|
File f = new File(filePath);
|
if(!f.exists()){
|
if(!f.getParentFile().exists()){
|
f.getParentFile().mkdirs();
|
}
|
try {
|
f.createNewFile();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return filePath;
|
}
|
|
}
|