Browse Source

fix:录音文件同步导oss

Joburgess 4 years ago
parent
commit
765e125b3d

+ 11 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/SoundCompareHelper.java

@@ -9,6 +9,7 @@ import com.ym.mec.biz.service.impl.SoundCompareHandler;
 import io.swagger.annotations.ApiModelProperty;
 import io.swagger.annotations.ApiModelProperty;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.CollectionUtils;
 
 
+import java.io.File;
 import java.io.RandomAccessFile;
 import java.io.RandomAccessFile;
 import java.math.BigDecimal;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.ArrayList;
@@ -37,6 +38,8 @@ public class SoundCompareHelper implements PitchDetectionHandler {
     @ApiModelProperty
     @ApiModelProperty
     private RandomAccessFile accessFile;
     private RandomAccessFile accessFile;
 
 
+    private File file;
+
     @ApiModelProperty(value = "录音文件地址")
     @ApiModelProperty(value = "录音文件地址")
     private String recordFilePath;
     private String recordFilePath;
 
 
@@ -120,6 +123,14 @@ public class SoundCompareHelper implements PitchDetectionHandler {
         return accessFile;
         return accessFile;
     }
     }
 
 
+    public File getFile() {
+        return file;
+    }
+
+    public void setFile(File file) {
+        this.file = file;
+    }
+
     public String getRecordFilePath() {
     public String getRecordFilePath() {
         return recordFilePath;
         return recordFilePath;
     }
     }

+ 21 - 4
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SoundCompareHandler.java

@@ -19,6 +19,9 @@ import com.ym.mec.biz.service.SysMusicCompareRecordService;
 import com.ym.mec.biz.service.WebSocketEventHandler;
 import com.ym.mec.biz.service.WebSocketEventHandler;
 import com.ym.mec.common.constant.CommonConstants;
 import com.ym.mec.common.constant.CommonConstants;
 import com.ym.mec.common.exception.BizException;
 import com.ym.mec.common.exception.BizException;
+import com.ym.mec.thirdparty.storage.StoragePluginContext;
+import com.ym.mec.thirdparty.storage.provider.KS3StoragePlugin;
+import com.ym.mec.util.upload.UploadUtil;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
@@ -64,6 +67,8 @@ public class SoundCompareHandler implements WebSocketEventHandler {
     public static final SoundCompareConfig soundCompareConfig = new SoundCompareConfig();
     public static final SoundCompareConfig soundCompareConfig = new SoundCompareConfig();
 
 
     @Autowired
     @Autowired
+    private StoragePluginContext storagePluginContext;
+    @Autowired
     private SysMusicCompareRecordService sysMusicCompareRecordService;
     private SysMusicCompareRecordService sysMusicCompareRecordService;
     @Autowired
     @Autowired
     private SysMusicScoreAccompanimentDao sysMusicScoreAccompanimentDao;
     private SysMusicScoreAccompanimentDao sysMusicScoreAccompanimentDao;
@@ -124,6 +129,7 @@ public class SoundCompareHandler implements WebSocketEventHandler {
                 }
                 }
                 try {
                 try {
                     File file = new File(tmpDir+phone + "_"+ userSoundInfoMap.get(phone).getMusicScoreId() +"_"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) +".wav");
                     File file = new File(tmpDir+phone + "_"+ userSoundInfoMap.get(phone).getMusicScoreId() +"_"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) +".wav");
+                    userSoundInfoMap.get(phone).setFile(file);
                     userSoundInfoMap.get(phone).setAccessFile(new RandomAccessFile(file, "rw"));
                     userSoundInfoMap.get(phone).setAccessFile(new RandomAccessFile(file, "rw"));
                     userSoundInfoMap.get(phone).setRecordFilePath(file.getAbsolutePath());
                     userSoundInfoMap.get(phone).setRecordFilePath(file.getAbsolutePath());
                 } catch (FileNotFoundException e) {
                 } catch (FileNotFoundException e) {
@@ -204,7 +210,6 @@ public class SoundCompareHandler implements WebSocketEventHandler {
     @Override
     @Override
     public void afterConnectionClosed(WebSocketSession session, String phone){
     public void afterConnectionClosed(WebSocketSession session, String phone){
         createHeader(phone);
         createHeader(phone);
-        userSoundInfoMap.remove(phone);
     }
     }
 
 
     /**
     /**
@@ -246,6 +251,21 @@ public class SoundCompareHandler implements WebSocketEventHandler {
             }
             }
             userSoundInfoMap.get(phone).setAccessFile(null);
             userSoundInfoMap.get(phone).setAccessFile(null);
         }
         }
+
+        String url = null;
+        try {
+            String folder = UploadUtil.getFileFloder();
+            url = storagePluginContext.uploadFile(KS3StoragePlugin.PLUGIN_NAME,"soundCompare/" + folder, userSoundInfoMap.get(phone).getFile());
+        } catch (Exception e) {
+            LOGGER.error("录音文件上传失败:{}", e);
+        }
+        userSoundInfoMap.get(phone).setRecordFilePath(url);
+        //存储评分数据
+        sysMusicCompareRecordService.saveMusicCompareData(phone, userSoundInfoMap.get(phone));
+
+        if(Objects.nonNull(userSoundInfoMap.get(phone).getFile())){
+            userSoundInfoMap.get(phone).getFile().deleteOnExit();
+        }
         userSoundInfoMap.remove(phone);
         userSoundInfoMap.remove(phone);
     }
     }
 
 
@@ -467,9 +487,6 @@ public class SoundCompareHandler implements WebSocketEventHandler {
         //计算分数并推送
         //计算分数并推送
         createPushInfo(phone, "overall", -1, intonation, cadence, integrity);
         createPushInfo(phone, "overall", -1, intonation, cadence, integrity);
 
 
-        //存储评分数据
-        sysMusicCompareRecordService.saveMusicCompareData(phone, userSoundInfoMap.get(phone));
-
         LOGGER.info("评分数据:{}", JSON.toJSONString(userSoundInfoMap.get(phone)));
         LOGGER.info("评分数据:{}", JSON.toJSONString(userSoundInfoMap.get(phone)));
     }
     }
 
 

+ 1 - 5
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SysMusicCompareRecordServiceImpl.java

@@ -50,11 +50,7 @@ public class SysMusicCompareRecordServiceImpl extends BaseServiceImpl<Long, SysM
 		sysMusicCompareRecord.setRecordFilePath(soundCompareInfo.getRecordFilePath());
 		sysMusicCompareRecord.setRecordFilePath(soundCompareInfo.getRecordFilePath());
 		sysMusicCompareRecord.setDeviceType(soundCompareInfo.getDeviceType());
 		sysMusicCompareRecord.setDeviceType(soundCompareInfo.getDeviceType());
 		sysMusicCompareRecord.setClientId(soundCompareInfo.getClientId());
 		sysMusicCompareRecord.setClientId(soundCompareInfo.getClientId());
-		try {
-			sysMusicCompareRecord.setPlayTime(soundCompareInfo.getAccessFile().length()/(SoundCompareHandler.soundCompareConfig.audioFormat.getFrameSize()*SoundCompareHandler.soundCompareConfig.audioFormat.getFrameRate()));
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
+		sysMusicCompareRecord.setPlayTime(soundCompareInfo.getFile().length()/(SoundCompareHandler.soundCompareConfig.audioFormat.getFrameSize()*SoundCompareHandler.soundCompareConfig.audioFormat.getFrameRate()));
 		sysMusicCompareRecordDao.insert(sysMusicCompareRecord);
 		sysMusicCompareRecordDao.insert(sysMusicCompareRecord);
 	}
 	}
 }
 }