录音播放功能
This commit is contained in:
@@ -9,9 +9,14 @@ import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -93,7 +98,119 @@ public class AudioManagementSegmentsController {
|
||||
return ResponseEntity.internalServerError().body(result);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 根据ID播放录音分段音频文件
|
||||
*/
|
||||
@PostMapping("/playSegmentById/{id}")
|
||||
@Operation(summary = "播放分段音频文件", description = "播放音频文件")
|
||||
public ResponseEntity<Resource> playSegmentById(
|
||||
@Parameter(description = "分段ID", required = true)
|
||||
@PathVariable String id) {
|
||||
try {
|
||||
AudioManagementSegments segment = audioManagementSegmentsService.getById(id);
|
||||
|
||||
if (segment == null) {
|
||||
log.warn("录音分段不存在,ID: {}", id);
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
String audioFilePath = segment.getAudioFilePath();
|
||||
if (audioFilePath == null || audioFilePath.trim().isEmpty()) {
|
||||
log.warn("录音分段文件路径为空,ID: {}", id);
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
File file = new File(audioFilePath);
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
log.warn("音频文件不存在或不是文件,路径: {}", audioFilePath);
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
// 检查文件是否为音频文件
|
||||
if (!isAudioFile(audioFilePath)) {
|
||||
log.warn("文件不是音频文件,路径: {}", audioFilePath);
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
Resource resource = new FileSystemResource(file);
|
||||
|
||||
// 设置响应头
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.parseMediaType(getContentType(audioFilePath)));
|
||||
headers.setContentLength(file.length());
|
||||
headers.set("Accept-Ranges", "bytes");
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.body(resource);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("播放录音分段音频文件异常,ID: {}, error: {}", id, e.getMessage(), e);
|
||||
return ResponseEntity.internalServerError().build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否为音频文件
|
||||
*/
|
||||
private boolean isAudioFile(String fileName) {
|
||||
if (fileName == null || fileName.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String extension = getFileExtension(fileName).toLowerCase();
|
||||
String[] audioExtensions = {"mp3", "wav", "m4a", "aac", "ogg", "flac", "wma"};
|
||||
|
||||
for (String ext : audioExtensions) {
|
||||
if (ext.equals(extension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件扩展名
|
||||
*/
|
||||
private String getFileExtension(String fileName) {
|
||||
if (fileName == null || fileName.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int lastDotIndex = fileName.lastIndexOf('.');
|
||||
if (lastDotIndex > 0 && lastDotIndex < fileName.length() - 1) {
|
||||
return fileName.substring(lastDotIndex + 1);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件名获取Content-Type
|
||||
*/
|
||||
private String getContentType(String fileName) {
|
||||
String extension = getFileExtension(fileName).toLowerCase();
|
||||
|
||||
switch (extension) {
|
||||
case "mp3":
|
||||
return "audio/mpeg";
|
||||
case "wav":
|
||||
return "audio/wav";
|
||||
case "m4a":
|
||||
return "audio/mp4";
|
||||
case "aac":
|
||||
return "audio/aac";
|
||||
case "ogg":
|
||||
return "audio/ogg";
|
||||
case "flac":
|
||||
return "audio/flac";
|
||||
case "wma":
|
||||
return "audio/x-ms-wma";
|
||||
default:
|
||||
return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 分页查询录音分段列表
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user