【问题标题】:Serve audio file with Spring boot使用 Spring Boot 提供音频文件
【发布时间】:2018-11-22 07:23:05
【问题描述】:

我正在使用带有 Undertow 的 Spring Boot 1.5 来提供 Angular 5 应用程序。它正在运行的应用程序,但我有以下问题。

我想在客户端播放音频文件,但我不能。在浏览器上,我可以确认音频文件存在,状态码为 206 Partial Content,但在需要时无法播放音频文件。当我从浏览器“http://localhost:8080/app/assets/audio/beep-sound.mp3”直接访问文件时,我可以收听音频。

此外,当我使用 angular-cli 为应用程序提供服务时,它的音频工作正常。 我是否需要在 Spring Boot 中进行任何配置才能启用此功能?

提前致谢

/*HTML file*
<audio #audioOption>
  <source src='assets/audio/beep-sound.mp3' type="audio/mp3">
</audio>

@ViewChild('audioOption') audioPlayerRef: ElementRef;
...
this.audioPlayerRef.nativeElement.play();

【问题讨论】:

    标签: spring angular spring-boot


    【解决方案1】:

    我找到了解决问题的方法。这是 Spring Boot 中所需要的。

    @Configuration
    @EnableConfigurationProperties({ ResourceProperties.class })
    public class StaticResourcesConfiguration extends WebMvcConfigurerAdapter {
    
        static final String[] STATIC_RESOURCES = new String[] { 
                "/**/*.css", 
                "/**/*.html", 
                "/**/*.js", 
                "/**/*.json",
                "/**/*.bmp",
                "/**/*.jpeg",
                "/**/*.jpg",
                "/**/*.png",
                "/**/*.ttf",
                "/**/*.eot",
                "/**/*.svg",
                "/**/*.woff",
                "/**/*.woff2",
                "/**/*.mp3"
        };
    
        @Autowired
        private ResourceProperties resourceProperties = new ResourceProperties();
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler(STATIC_RESOURCES).addResourceLocations(resourceProperties.getStaticLocations());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 2015-09-05
      • 2019-08-29
      • 2017-01-21
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多