Vaadin audio element doesn't work when I use it to play .m4a sound files (Vaadin 7.3.2)

不羁的心 提交于 2019-12-10 16:52:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!