我想使用户能够播放声音。我执行正常工作与Firefox浏览器。在Safari声音不播放。我证实,该音频控制与其他网站的Safari浏览器的工作原理。所以,我认为,我将不得不改变我的控制器的东西吗?
控制器:
@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>
大多数球员都需要支持部分内容请求(或字节范围)的控制器。
这可能是一个有点棘手来实现,所以我会建议使用像Spring社区项目Spring Content那么你就不需要担心如何在所有实现控制器。的概念和编程模型非常相似,春季数据的,通过它的长相,你已经在使用。
假设你正在使用Spring启动(让我知道如果你是不是),那么它会是这个样子:
pom.hml
<!-- 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>
spring boot application.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> {
//
}
}
Chara胎儿.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功能以及;创建== POST,阅读== GET(包括你所需要的字节范围的支持),更新== PUT,DELETE删除==在对你有用的情况。上传的声音将被保存在“/路径/到/你/的声音”。
所以...
GET /characterSounds/{characterId}
将返回部分内容的响应,这应该在最适当的流,如果不是所有的球员(包括寻求向前和向后)。
HTH