html - 如何使用 Spring Boot 流式传输音频

标签 html spring-boot spring-mvc audio stream

我想让用户播放声音。我的实现在 Firefox 中运行良好。在 Safari 上不播放声音。我证实,音频控制在 safari 中与其他网站一起工作。所以,我假设,我必须在我的 Controller 中改变一些东西?

Controller :

@RequestMapping(value = "/sound/character/get/{characterId}", method = RequestMethod.GET, produces = {
            MediaType.APPLICATION_OCTET_STREAM_VALUE })
        public ResponseEntity playAudio(HttpServletRequest request,HttpServletResponse response, @PathVariable("characterId") int characterId) throws FileNotFoundException{

        logger.debug("[downloadRecipientFile]");

        de.tki.chinese.entity.Character character = characterRepository.findById(characterId);
        String file = UPLOADED_FOLDER + character.getSoundFile();

        long length = new File(file).length();


        InputStreamResource inputStreamResource = new InputStreamResource( new FileInputStream(file));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentLength(length);
        httpHeaders.setCacheControl(CacheControl.noCache().getHeaderValue());
        return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
    }

看法
        <audio id="voice" controls="">
            <source src="/sound/character/get/2">
        </audio>

火狐(工作正常):
Firefox (works fine)

Safari(不工作):
Safari (not working!)

最佳答案

大多数播放器将需要一个支持部分内容请求(或字节范围)的 Controller 。

这可能有点难以实现,所以我建议使用类似 Spring 社区项目 Spring Content 的东西。那么你根本不需要担心如何实现 Controller 。概念和编程模型与 Spring Data 非常相似,从外观上看,您已经在使用它。

假设您正在使用 Spring Boot(如果您没有使用,请告诉我),那么它看起来像这样:

pom.xml


<!-- Java API -->
<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>content-fs-spring-boot-starter</artifactId>
    <version>0.6.0</version>
</dependency>
<!-- REST API -->
<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>spring-content-rest-boot-starter</artifactId>
    <version>0.6.0</version>
</dependency>

SpringBootApplication.java


@SpringBootApplication
public class YourSpringBootApplication {

  public static void main(String[] args) {
    SpringApplication.run(YourSpringBootApplication.class, args);
  }

  @Configuration
  @EnableFilesystemStores
  public static class StorageConfig {
    File filesystemRoot() {
        return new File("/path/to/your/sounds");
    }

    @Bean
    public FileSystemResourceLoader fsResourceLoader() throws Exception {
      return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
    }
  }

  @StoreRestResource(path="characterSounds")
  public interface SoundsContentStore extends ContentStore<UUUID,String> {
    //
  }
}

Charater.java


public class Character {

    @Id
    @GeneratedValue
    private Long id;

    ...other existing fields...

    @ContentId
    private UUID contentId;

    @ContentLength
    private Long contnetLength;

    @MimeType
    private String mimeType;
} 

这是在 /characterSounds 创建基于 REST 的音频服务所需的全部内容。支持流媒体。它实际上也支持完整的 CRUD 功能; Create == POST, Read == GET(包括您需要的字节范围支持),Update == PUT,Delete == DELETE,以防对您有用。上传的声音将存储在“/path/to/your/sounds”中。

所以...
GET /characterSounds/{characterId}
将返回部分内容响应,并且这应该在大多数(如果不是全部)播放器中正确传输(包括向前和向后搜索)。

HTH

关于html - 如何使用 Spring Boot 流式传输音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54569561/

相关文章:

php - 使用 HTML 和 PHP 时无法将电子邮件插入 MySQL 表

java - 超链接下拉列表

html - 仅在 IE 中位置不正确

javascript - 聚合物 dom 模块什么都不显示

javascript - 如何在按下回车键时触发 HTML 密码输入

java - "The type org.springframework.web.WebApplicationInitializer cannot be resolved. It is indirectly referenced from required .class files"

mysql - 如何使用 Spring Boot Rest API 在移动应用程序(React Native 或 Ionic)中获取推送通知

java - 如何在Spring集成中推迟消息的消费

java - Spring Security 用户身份验证不起作用

spring-mvc - 两个 DispatcherServlet 和 DelegatingFilterProxy 不能一起工作