package FXMediaPlayer; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import javafx.scene.media.MediaInputStream; public class MediaInputStreamHelper { private static MediaInputStream.StreamType streamType = MediaInputStream.StreamType.AUTO_DETECT; public static MediaInputStream getMediaInputStream(String source) { File file; InputStream inputStream; long length; try { if (source.startsWith("file:")) { file = new File(new URI(source)); inputStream = new FileInputStream(file); length = file.length(); } else if (source.startsWith("http")) { URI uri = new URI(source); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); inputStream = connection.getInputStream(); length = connection.getContentLengthLong(); } else { return null; } return new MediaInputStream(inputStream, streamType, length); } catch (URISyntaxException ex) { System.err.println(ex.toString()); } catch (FileNotFoundException ex) { System.err.println(ex.toString()); } catch (MalformedURLException ex) { System.err.println(ex.toString()); } catch (IOException ex) { System.err.println(ex.toString()); } return null; } public static MediaInputStream getAdvancedMediaInputStream(String source) { if (source.startsWith("file:")) { return new _AdvancedFileMediaInputStream(source); } else if (source.startsWith("http")) { return null; } return null; } private static class _AdvancedFileMediaInputStream extends MediaInputStream { private final Object lock = new Object(); private RandomAccessFile file = null; private FileChannel channel = null; private ByteBuffer readBuffer = null; private _AdvancedFileMediaInputStream(String source) { try { file = new RandomAccessFile(new File(new URI(source)), "r"); channel = file.getChannel(); setLength(file.length()); setEnableProgressBuffer(false); } catch (FileNotFoundException ex) { System.err.println(ex.toString()); } catch (IOException ex) { System.err.println(ex.toString()); } catch (URISyntaxException ex) { System.err.println(ex.toString()); } } @Override public int read(byte[] data) throws IOException { synchronized (lock) { if (readBuffer == null || readBuffer.capacity() < data.length) { readBuffer = ByteBuffer.allocate(data.length); } int read; readBuffer.rewind(); readBuffer.limit(data.length); read = channel.read(readBuffer); readBuffer.rewind(); readBuffer.get(data); return read; } } @Override public synchronized int read(byte[] data, long position) throws IOException { synchronized (lock) { if (readBuffer == null || readBuffer.capacity() < data.length) { readBuffer = ByteBuffer.allocate(data.length); } int read; readBuffer.rewind(); readBuffer.limit(data.length); read = channel.read(readBuffer, position); readBuffer.rewind(); readBuffer.get(data); return read; } } @Override public boolean seek(long position) throws IOException { synchronized (lock) { channel.position(position); return true; } } @Override public void close() throws IOException { synchronized (lock) { if (channel != null) { channel.close(); channel = null; } if (file != null) { file.close(); file = null; } } } } }