feat(music): 添加音乐时长获取功能
- 在 FileService接口中添加 getMusicDuration 方法- 在 FileServiceImpl 类中实现音乐时长获取逻辑 - 在 MusicServiceImpl 中使用该功能来设置音乐时长 - 移除 application.properties 中的注释内容 - 添加 jaudiotagger 依赖用于获取音乐文件信息
This commit is contained in:
@@ -33,12 +33,12 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 开发工具 -->
|
<!-- 开发工具 -->
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>org.springframework.boot</groupId>
|
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<!-- <artifactId>spring-boot-devtools</artifactId>-->
|
||||||
<scope>runtime</scope>
|
<!-- <scope>runtime</scope>-->
|
||||||
<optional>true</optional>
|
<!-- <optional>true</optional>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!-- Lombok -->
|
<!-- Lombok -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -123,6 +123,13 @@
|
|||||||
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
||||||
<version>4.5.0</version>
|
<version>4.5.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 获取音乐文件信息 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org</groupId>
|
||||||
|
<artifactId>jaudiotagger</artifactId>
|
||||||
|
<version>2.0.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -142,4 +149,4 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -38,4 +38,12 @@ public interface FileService {
|
|||||||
* @return 是否成功
|
* @return 是否成功
|
||||||
*/
|
*/
|
||||||
boolean deleteFile(String url);
|
boolean deleteFile(String url);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取音乐时长
|
||||||
|
*
|
||||||
|
* @param file 音乐文件
|
||||||
|
* @return 时长(秒)
|
||||||
|
*/
|
||||||
|
int getMusicDuration(MultipartFile file);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.test.musichouduan.service.impl;
|
|||||||
import com.test.musichouduan.dto.Result;
|
import com.test.musichouduan.dto.Result;
|
||||||
import com.test.musichouduan.exception.BusinessException;
|
import com.test.musichouduan.exception.BusinessException;
|
||||||
import com.test.musichouduan.service.FileService;
|
import com.test.musichouduan.service.FileService;
|
||||||
|
import org.jaudiotagger.audio.AudioFile;
|
||||||
|
import org.jaudiotagger.audio.AudioFileIO;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -75,6 +77,33 @@ public class FileServiceImpl implements FileService {
|
|||||||
return uploadFile(file, "musics");
|
return uploadFile(file, "musics");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMusicDuration(MultipartFile file) {
|
||||||
|
if (file == null || file.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
File tempFile = null;
|
||||||
|
try {
|
||||||
|
byte[] bytes = file.getBytes();
|
||||||
|
// 使用原始文件扩展名创建临时文件
|
||||||
|
String originalFilename = file.getOriginalFilename();
|
||||||
|
String extension = getFileExtension(originalFilename);
|
||||||
|
tempFile = File.createTempFile("music-duration-", "." + extension);
|
||||||
|
Files.write(tempFile.toPath(), bytes);
|
||||||
|
|
||||||
|
AudioFile audioFile = AudioFileIO.read(tempFile);
|
||||||
|
int duration = audioFile.getAudioHeader().getTrackLength();
|
||||||
|
return duration;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("获取音乐时长失败", e);
|
||||||
|
return 0;
|
||||||
|
} finally {
|
||||||
|
if (tempFile != null) {
|
||||||
|
tempFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uploadLyric(MultipartFile file) {
|
public String uploadLyric(MultipartFile file) {
|
||||||
// 检查文件是否为空
|
// 检查文件是否为空
|
||||||
|
|||||||
@@ -283,6 +283,9 @@ public class MusicServiceImpl implements MusicService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public MusicDTO addMusic(MusicDTO musicDTO, MultipartFile file, MultipartFile cover) {
|
public MusicDTO addMusic(MusicDTO musicDTO, MultipartFile file, MultipartFile cover) {
|
||||||
|
// 获取音乐时长
|
||||||
|
int duration = fileService.getMusicDuration(file);
|
||||||
|
|
||||||
// 上传音乐文件
|
// 上传音乐文件
|
||||||
String musicUrl = fileService.uploadMusic(file);
|
String musicUrl = fileService.uploadMusic(file);
|
||||||
|
|
||||||
@@ -294,6 +297,7 @@ public class MusicServiceImpl implements MusicService {
|
|||||||
BeanUtils.copyProperties(musicDTO, music);
|
BeanUtils.copyProperties(musicDTO, music);
|
||||||
music.setUrl(musicUrl);
|
music.setUrl(musicUrl);
|
||||||
music.setCover(coverUrl);
|
music.setCover(coverUrl);
|
||||||
|
music.setDuration(duration);
|
||||||
music.setPlayCount(0L);
|
music.setPlayCount(0L);
|
||||||
|
|
||||||
// 设置歌词
|
// 设置歌词
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
#spring.application.name=music-houduan
|
|
||||||
#server.port=8085
|
|
||||||
#
|
|
||||||
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/music_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
|
||||||
#spring.datasource.username=root
|
|
||||||
#spring.datasource.password=123456
|
|
||||||
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
|
||||||
#
|
|
||||||
#spring.jpa.hibernate.ddl-auto=update
|
|
||||||
#spring.jpa.show-sql=true
|
|
||||||
#
|
|
||||||
#spring.data.redis.host=127.0.0.1
|
|
||||||
#spring.data.redis.port=6379
|
|
||||||
#spring.data.redis.password=123456
|
|
||||||
#
|
|
||||||
#sa-token.token-name=satoken
|
|
||||||
#sa-token.timeout=2592000
|
|
||||||
#sa-token.is-concurrent=true
|
|
||||||
#sa-token.is-share=true
|
|
||||||
#sa-token.token-style=uuid
|
|
||||||
#
|
|
||||||
## 文件上传配置
|
|
||||||
#file.upload-path=./upload/
|
|
||||||
#project.root-path=.
|
|
||||||
#file.access-path=/upload/
|
|
||||||
#file.allowed-types=jpg,jpeg,png,gif,mp3,wav,lrc,txt
|
|
||||||
Reference in New Issue
Block a user