问题
I use this to display html5 audio in one of my Vaadin application. When I simply requst the data in browser, and save the file, it can be played - but it always fails to do so, when I try it in Vaadin.
Can you point out, what am I doing wrong?
public class AudioArea extends Audio {
public AudioArea(final int soundId) {
super();
StreamResource resource = new StreamResource(
new StreamResource.StreamSource() {
public InputStream getStream() {
byte[] data = MyVaadinUI.request.getBinaryData(
soundId, BinaryDataSource.TYPE_SOUND, 0, 0);
if (data == null) {
return null;
}
return new ByteArrayInputStream(data);
}
}, "");
setSource(resource);
markAsDirty();
}
}
回答1:
Are you sure that your browser supports the format? If you try it with plain html can you play it?
If the browser supports the format, then I suggest this:
Audio audio = new Audio("MyAudio");
audio.setSource(new ThemeResource("audio/myAudio.m4a"));
myLayout.addComponent(audio);
And you could put the file under your theme folder (or you can use FileResource
instead).
EDIT
Maybe the missing MIME Type is the problem:
StreamResource resource = new StreamResource(
new StreamResource.StreamSource() {
public InputStream getStream() {
byte[] data = MyVaadinUI.request.getBinaryData(
soundId, BinaryDataSource.TYPE_SOUND, 0, 0);
if (data == null) {
return null;
}
return new ByteArrayInputStream(data);
}
}, "") {
@Override
public String getMIMEType() {
return "audio/mp4";
}
};
来源:https://stackoverflow.com/questions/26406923/vaadin-audio-element-doesnt-work-when-i-use-it-to-play-m4a-sound-files-vaadin