| | |
| | | import com.dji.sample.speak.model.enums.VoiceEnums; |
| | | import com.dji.sample.speak.service.SpeakVoiceService; |
| | | import com.dji.sample.speak.util.MD5Util; |
| | | import com.dji.sample.territory.service.impl.TbFjServiceImpl; |
| | | import io.minio.MinioClient; |
| | | import io.minio.PutObjectArgs; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | import javax.sound.sampled.AudioInputStream; |
| | | import javax.sound.sampled.AudioSystem; |
| | | import javax.sound.sampled.UnsupportedAudioFileException; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.*; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | |
| | | |
| | | @Override |
| | | public int restartVoice(String sn, Integer psdk_index) { |
| | | SpeakRestartDto dto=new SpeakRestartDto(); |
| | | SpeakRestartDto dto = new SpeakRestartDto(); |
| | | dto.setPsdk_index(psdk_index); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_REPLAY.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int stopVoice(String sn, Integer psdk_index) { |
| | | SpeakStopDto dto=new SpeakStopDto(); |
| | | SpeakStopDto dto = new SpeakStopDto(); |
| | | dto.setPsdk_index(psdk_index); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_STOP.getMethod(), dto).getResult(); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PaginationData<SpeakVoiceEntity> getVoices(Integer pages,Integer page_size) { |
| | | public PaginationData<SpeakVoiceEntity> getVoices(Integer pages, Integer page_size) { |
| | | // Paging Query |
| | | Page<SpeakVoiceEntity> page = voiceMapper.selectPage( |
| | | new Page<>(pages,page_size), |
| | | new Page<>(pages, page_size), |
| | | new LambdaQueryWrapper<SpeakVoiceEntity>() |
| | | ); |
| | | // Wrap the results of a paging query into a custom paging object. |
| | |
| | | String secretKey = pojo.getSecretKey(); |
| | | String bucketName = pojo.getBucket(); |
| | | String objectName = "/" + fileName + getNowTimeName(); |
| | | FileServiceImpl.uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, file, "audio/mpeg"); |
| | | FileServiceImpl.uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, file, "audio/wav"); |
| | | return endpoint + "/" + bucketName + objectName; |
| | | } |
| | | |
| | | |
| | | public static String getNowTimeName() { |
| | | LocalDateTime currentTime = LocalDateTime.now(); |
| | |
| | | int frameSize = format.getFrameSize(); |
| | | float frameRate = format.getFrameRate(); |
| | | return Math.round(audioFileLength / (frameSize * frameRate)); |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | return 0.0; |
| | | } |
| | | } |
| | | |
| | | public int takeVoices(String sn, Integer psdk_index, String name, String url) { |
| | | SpeakVoiceEntity voiceEntity = new SpeakVoiceEntity(); |
| | | SpeakVoiceStartDto dto = new SpeakVoiceStartDto(); |
| | | SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto(); |
| | | SpeakVolumeDto volumeDto = new SpeakVolumeDto(); |
| | | File file = TbFjServiceImpl.downloadFile(url); |
| | | String md5 = MD5Util.getMD5Checksum(file); |
| | | volumeDto.setPsdk_index(psdk_index); |
| | | volumeDto.setPlay_volume(100); |
| | | String names = name + ".wav"; |
| | | fileDto.setUrl(url); |
| | | fileDto.setMd5(md5); |
| | | fileDto.setFormat(FormatEnum.PCM); |
| | | fileDto.setName(name); |
| | | dto.setPsdk_index(psdk_index); |
| | | dto.setFile(fileDto); |
| | | voiceEntity.setMd5(md5); |
| | | voiceEntity.setUrl(url); |
| | | voiceEntity.setName(names); |
| | | voiceMapper.insert(voiceEntity); |
| | | messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), volumeDto); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | public void saveFileToLocal(File file, String destinationPath) throws IOException { |
| | | // 创建目标文件对象 |
| | | File destinationFile = new File(destinationPath); |
| | | |
| | | // 确保目标文件的父目录存在 |
| | | destinationFile.getParentFile().mkdirs(); |
| | | |
| | | // 将文件内容写入目标文件 |
| | | try (FileOutputStream outputStream = new FileOutputStream(destinationFile); |
| | | InputStream inputStream = new FileInputStream(file)) { |
| | | byte[] buffer = new byte[1024]; |
| | | int bytesRead; |
| | | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | } |
| | | } |
| | | } |