I 'm using this library to implement an audio playback in vaadin 8: https://vaadin.com/directory/component/audiovideo
This is the code of my view. Only a layout and a GwatAudio instance:
public void init() {
layout = new VerticalLayout();
String filename = "Ensoniq-3.wav";
String pathName = "C:\\PERSEO\\TADPLAYER\\";
ConnectorAudio con = new ConnectorAudio(filename, pathName);
GwtAudio atc = new GwtAudio(con);
layout.addComponent(atc);
}
And this is the resource subclass that I need to create a Gwtaudio
public class ConnectorAudio implements ConnectorResource {
/**
*
*/
private static final long serialVersionUID = 8806461248248864379L;
private String fileName;
private StreamResource resource;
public ConnectorAudio(String fileName, String pathName) {
// TODO Auto-generated constructor stub
this.fileName = fileName;
StreamSource stream = new StreamSource() {
@Override
public InputStream getStream() {
try {
return new FileInputStream(pathName + fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
};
this.resource = new StreamResource(stream, this.fileName );
}
@Override
public String getMIMEType() {
// TODO Auto-generated method stub
return this.resource.getMIMEType();
}
@Override
public DownloadStream getStream() {
// TODO Auto-generated method stub
return resource.getStream();
}
@Override
public String getFilename() {
// TODO Auto-generated method stub
return this.resource.getFilename();
}
public Resource getResource() {
// TODO Auto-generated method stub
return this.resource;
}
}
The audio is even working, but not properly, and when the view is loaded, always this exception is triggered:
I think there must be a problem related to wildFly 13 , because of the exception that is triggered when I try to create an instance of gwt audio.
In order to seek and streaming to work correctly, component needs to know the length of the file. In Vaadin 8, plain ConnectorResource
does not give that information.
You have two alternatives. The FileResource
has that capability, and if it fits your case (which I think so, as your example have file path), you should use that.
File file = new File("C:\\PERSEO\\TADPLAYER\\Ensoniq-3.wav");
FileResouce fileResource = new FileResource(file);
audio.setSource(fileResource);
By code inspection your ConnectorAudio
is attempt of recreating FileResource
that already exists, but you miss this one feature of setting the content length.
For other scenarios, the add-on comes with ContentLengthConnectorResource
wrapper, so that it is possible to use e.g. ClassResource
, if you know by other means the content-length.
ClassResource audioResource = new ClassResource("/test.mp3");
long length = 725240;
audio.setSource(new ContentLengthConnectorResource(audioResource,length));