在Linux服务器上运行Java代码时,如果涉及到音频处理,可能会遇到一些在Windows上不存在的问题。尤其是在处理音频文件时,javax.sound.sampled.Clip
接口在Linux上的兼容性可能会有问题。这是因为Clip
依赖于底层的音频系统,而这些系统在不同的操作系统之间实现不同。
该方法在windows上正常
private String getWavDuration(MultipartFile file) {try (BufferedInputStream bis = new BufferedInputStream(file.getInputStream())) {Clip clip = AudioSystem.getClip();clip.open(AudioSystem.getAudioInputStream(bis));long durationInMillis = (long) (clip.getMicrosecondLength() / 1000);long seconds = TimeUnit.MILLISECONDS.toSeconds(durationInMillis);return seconds + "s";} catch (Exception e) {e.printStackTrace();throw new RuntimeException("无法获取 WAV 文件时长", e);}}
错误信息
22:57:31.345 [http-nio-8081-exec-85] DEBUG c.r.w.m.T.insertTReportDecibels - [debug,135] - <== Updates: 1
java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian is supported.at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:479)at javax.sound.sampled.AudioSystem.getClip(AudioSystem.java:522)at com.ruoyi.webapp.controller.ReportController.getWavDuration(ReportController.java:1486)at com.ruoyi.webapp.controller.ReportController.reportUpload(ReportController.java:65)at com.ruoyi.webapp.controller.ReportController$$FastClassBySpringCGLIB$$3e617660.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)at org.springframework.aop.framework.CglibAopProxy.invokeMethod(CglibAopProxy.java:386)at org.springframework.aop.framework.CglibAopProxy.access$000(CglibAopProxy.java:85)at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:704)at com.ruoyi.webapp.controller.ReportController$$EnhancerBySpringCGLIB$$3cc5caf2.reportUpload(<generated>)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1070)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:111)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
但是到Linux上报错
<!--Linux上获取wav时长报错之后 引入的包--><!-- https://mvnrepository.com/artifact/com.github.dragoon000320/tarsosdsp --><dependency><groupId>com.github.dragoon000320</groupId><artifactId>tarsosdsp</artifactId><version>1.0</version></dependency>
private String getWavDuration3(MultipartFile file) throws UnsupportedAudioFileException, IOException {try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(file.getBytes()))) {AudioFormat format = audioInputStream.getFormat();long frames = audioInputStream.getFrameLength();double durationInSeconds = (frames + 0.0) / format.getFrameRate();int durationInSecondsInt = (int) Math.round(durationInSeconds);return durationInSecondsInt + "s";}}