use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.
the class JavaFXSEPort method createMediaAsync.
@Override
public AsyncResource<Media> createMediaAsync(String uriAddress, final boolean isVideo, final Runnable onCompletion) {
final AsyncResource<Media> out = new AsyncResource<Media>();
if (!checkForPermission("android.permission.READ_PHONE_STATE", "This is required to play media")) {
out.error(new IOException("android.permission.READ_PHONE_STATE is required to play media"));
return out;
}
if (!checkForPermission("android.permission.WRITE_EXTERNAL_STORAGE", "This is required to play media")) {
out.error(new IOException("android.permission.WRITE_EXTERNAL_STORAGE is required to play media"));
return out;
}
if (uriAddress.startsWith("file:")) {
uriAddress = unfile(uriAddress);
}
final String uri = uriAddress;
if (!fxExists) {
String msg = "This fetaure is supported from Java version 1.7.0_06, update your Java to enable this feature. This might fail on OpenJDK as well in which case you will need to install the Oracle JDK. ";
System.out.println(msg);
out.error(new IOException(msg));
return out;
}
java.awt.Container cnt = canvas.getParent();
while (!(cnt instanceof JFrame)) {
cnt = cnt.getParent();
if (cnt == null) {
out.error(new RuntimeException("Could not find canvas. Cannot create media"));
return out;
}
}
final java.awt.Container c = cnt;
// final Media[] media = new Media[1];
final Exception[] err = new Exception[1];
final javafx.embed.swing.JFXPanel m = new CN1JFXPanel();
// mediaContainer = m;
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
if (uri.indexOf(':') < 0 && uri.lastIndexOf('/') == 0) {
String mimeType = "video/mp4";
new CodenameOneMediaPlayer(getResourceAsStream(getClass(), uri), mimeType, (JFrame) c, m, onCompletion, out);
return;
}
new CodenameOneMediaPlayer(uri, isVideo, (JFrame) c, m, onCompletion, out);
} catch (Exception ex) {
out.error(ex);
}
}
});
return out;
}
use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.
the class SEBrowserComponent method captureScreenshot.
/**
* Captures a browser screenshot using the JavaFX WebView snapshot() method. The benefit
* of this approach over {@link Component#toImage()} is that this can produce an image
* with transparency.
* @return AsyncResource resolving to an image.
*/
public AsyncResource<Image> captureScreenshot() {
final AsyncResource<Image> out = new AsyncResource<Image>();
EventQueue.invokeLater(new Runnable() {
public void run() {
panel.repaint();
Platform.runLater(new Runnable() {
public void run() {
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
web.snapshot(new javafx.util.Callback<SnapshotResult, Void>() {
@Override
public Void call(SnapshotResult p) {
WritableImage wi = p.getImage();
BufferedImage img = SwingFXUtils.fromFXImage(wi, null);
out.complete(Image.createImage(img));
return null;
}
}, params, null);
}
});
}
});
return out;
}
use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.
the class JavaCEFSEPort method createMediaAsync.
@Override
public AsyncResource<Media> createMediaAsync(String uriAddress, boolean isVideo, Runnable onCompletion) {
final AsyncResource<Media> out = new AsyncResource<Media>();
if (!checkForPermission("android.permission.READ_PHONE_STATE", "This is required to play media")) {
out.error(new IOException("android.permission.READ_PHONE_STATE is required to play media"));
return out;
}
if (!checkForPermission("android.permission.WRITE_EXTERNAL_STORAGE", "This is required to play media")) {
out.error(new IOException("android.permission.WRITE_EXTERNAL_STORAGE is required to play media"));
return out;
}
java.awt.Container cnt = canvas.getParent();
while (!(cnt instanceof JFrame)) {
cnt = cnt.getParent();
if (cnt == null) {
out.error(new RuntimeException("Could not find canvas. Cannot create media"));
return out;
}
}
if (uriAddress.startsWith("file:")) {
uriAddress = unfile(uriAddress);
File f = new File(uriAddress);
if (f.exists()) {
try {
FileInputStream fis = new FileInputStream(f);
StreamWrapper stream = new StreamWrapper(fis, getMimetype(f), f.length());
String id = BrowserPanel.getStreamRegistry().registerStream(stream);
// uriAddress = InputStreamSchemeHandler.getURL(id);
uriAddress = "https://cn1app/streams/" + id;
// new CodenameOneMediaPlayer(stream, ((JFrame)cnt), onCompletion, out);
} catch (IOException ex) {
out.error(ex);
return out;
}
// uriAddress = "file://"+f.getAbsolutePath().replace('\\', '/');
} else {
out.error(new FileNotFoundException(uriAddress));
return out;
}
}
final String uri = uriAddress;
try {
new CodenameOneMediaPlayer(uri, isVideo, (JFrame) cnt, onCompletion, out);
} catch (IOException ex) {
out.error(ex);
}
return out;
}
use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.
the class JavaCEFSEPort method createMediaAsync.
@Override
public AsyncResource<Media> createMediaAsync(InputStream inputStream, String mimeType, Runnable onCompletion) {
final AsyncResource<Media> out = new AsyncResource<Media>();
if (!checkForPermission("android.permission.READ_PHONE_STATE", "This is required to play media")) {
out.error(new IOException("android.permission.READ_PHONE_STATE is required to play media"));
return out;
}
if (!checkForPermission("android.permission.WRITE_EXTERNAL_STORAGE", "This is required to play media")) {
out.error(new IOException("android.permission.WRITE_EXTERNAL_STORAGE is required to play media"));
return out;
}
java.awt.Container cnt = canvas.getParent();
while (!(cnt instanceof JFrame)) {
cnt = cnt.getParent();
if (cnt == null) {
out.error(new RuntimeException("Could not find canvas. Cannot create media"));
return out;
}
}
StreamWrapper stream = new StreamWrapper(inputStream, mimeType, -1);
String id = BrowserPanel.getStreamRegistry().registerStream(stream);
String uriAddress = "https://cn1app/streams/" + id;
try {
new CodenameOneMediaPlayer(uriAddress, mimeType.startsWith("video/"), (JFrame) cnt, onCompletion, out);
} catch (IOException ex) {
out.error(ex);
}
return out;
}
use of com.codename1.util.AsyncResource in project CodeRAD by shannah.
the class ParsingService method parseXML.
public AsyncResource<Element> parseXML(Reader content, XMLParser parser) {
AsyncResource<Element> out = new AsyncResource<Element>();
start();
thread.run(() -> {
try {
Element el = parser.parse(content);
if (autoCloseStreams)
content.close();
out.complete(el);
} catch (Throwable ex) {
out.error(ex);
}
});
return out;
}
Aggregations