Search in sources :

Example 16 with AsyncResource

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;
}
Also used : AbstractMedia(com.codename1.media.AbstractMedia) AsyncMedia(com.codename1.media.AsyncMedia) Media(com.codename1.media.Media) IOException(java.io.IOException) IOException(java.io.IOException) JFrame(javax.swing.JFrame) AsyncResource(com.codename1.util.AsyncResource)

Example 17 with AsyncResource

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;
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) SnapshotResult(javafx.scene.SnapshotResult) BufferedImage(java.awt.image.BufferedImage) WritableImage(javafx.scene.image.WritableImage) AsyncResource(com.codename1.util.AsyncResource) BufferedImage(java.awt.image.BufferedImage)

Example 18 with AsyncResource

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;
}
Also used : AbstractMedia(com.codename1.media.AbstractMedia) AsyncMedia(com.codename1.media.AsyncMedia) Media(com.codename1.media.Media) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JFrame(javax.swing.JFrame) AsyncResource(com.codename1.util.AsyncResource) File(java.io.File)

Example 19 with AsyncResource

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;
}
Also used : JFrame(javax.swing.JFrame) AbstractMedia(com.codename1.media.AbstractMedia) AsyncMedia(com.codename1.media.AsyncMedia) Media(com.codename1.media.Media) IOException(java.io.IOException) AsyncResource(com.codename1.util.AsyncResource)

Example 20 with AsyncResource

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;
}
Also used : Element(com.codename1.xml.Element) AsyncResource(com.codename1.util.AsyncResource)

Aggregations

AsyncResource (com.codename1.util.AsyncResource)20 IOException (java.io.IOException)6 Timer (java.util.Timer)5 AbstractMedia (com.codename1.media.AbstractMedia)4 AsyncMedia (com.codename1.media.AsyncMedia)4 Media (com.codename1.media.Media)4 Map (java.util.Map)4 JFrame (javax.swing.JFrame)4 Element (com.codename1.xml.Element)3 InputStreamReader (java.io.InputStreamReader)3 Component (com.codename1.ui.Component)2 Form (com.codename1.ui.Form)2 Toolbar (com.codename1.ui.Toolbar)2 Motion (com.codename1.ui.animations.Motion)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 StringReader (java.io.StringReader)2 TimerTask (java.util.TimerTask)2 BillingResult (com.android.billingclient.api.BillingResult)1