Example usage for android.widget VideoView setZOrderMediaOverlay

List of usage examples for android.widget VideoView setZOrderMediaOverlay

Introduction

In this page you can find the example usage for android.widget VideoView setZOrderMediaOverlay.

Prototype

public void setZOrderMediaOverlay(boolean isMediaOverlay) 

Source Link

Document

Control whether the surface view's surface is placed on top of another regular surface view in the window (but still behind the window itself).

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc//from   w  ww  .  j  a  va  2 s . c  o m
 */
@Override
public Media createMedia(final String uri, boolean isVideo, final Runnable onCompletion) throws IOException {
    if (getActivity() == null) {
        return null;
    }
    if (!uri.startsWith(FileSystemStorage.getInstance().getAppHomePath())) {
        if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")) {
            return null;
        }
    }
    if (uri.startsWith("file://")) {
        return createMedia(removeFilePrefix(uri), isVideo, onCompletion);
    }
    File file = null;
    if (uri.indexOf(':') < 0) {
        // use a file object to play to try and workaround this issue:
        // http://code.google.com/p/android/issues/detail?id=4124
        file = new File(uri);
    }

    Media retVal;

    if (isVideo) {
        final AndroidImplementation.Video[] video = new AndroidImplementation.Video[1];
        final boolean[] flag = new boolean[1];
        final File f = file;
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                VideoView v = new VideoView(getActivity());
                v.setZOrderMediaOverlay(true);
                if (f != null) {
                    v.setVideoURI(Uri.fromFile(f));
                } else {
                    v.setVideoURI(Uri.parse(uri));
                }
                video[0] = new AndroidImplementation.Video(v, getActivity(), onCompletion);
                flag[0] = true;
                synchronized (flag) {
                    flag.notify();
                }
            }
        });
        while (!flag[0]) {
            synchronized (flag) {
                try {
                    flag.wait(100);
                } catch (InterruptedException ex) {
                }
            }
        }
        return video[0];
    } else {
        MediaPlayer player;
        if (file != null) {
            FileInputStream is = new FileInputStream(file);
            player = new MediaPlayer();
            player.setDataSource(is.getFD());
            player.prepare();
        } else {
            player = MediaPlayer.create(getActivity(), Uri.parse(uri));
        }
        retVal = new Audio(getActivity(), player, null, onCompletion);
    }
    return retVal;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc/*from   w w w .  j a va2s  . co m*/
 */
@Override
public Media createMedia(InputStream stream, String mimeType, final Runnable onCompletion) throws IOException {
    if (getActivity() == null) {
        return null;
    }
    /*if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")){
    return null;
    }*/
    boolean isVideo = mimeType.contains("video");

    if (!isVideo && stream instanceof FileInputStream) {
        MediaPlayer player = new MediaPlayer();
        player.setDataSource(((FileInputStream) stream).getFD());
        player.prepare();
        return new Audio(getActivity(), player, stream, onCompletion);
    }
    String extension = MimeTypeMap.getFileExtensionFromUrl(mimeType);
    final File temp = File.createTempFile("mtmp", extension == null ? "dat" : extension);
    temp.deleteOnExit();
    OutputStream out = createFileOuputStream(temp);

    byte buf[] = new byte[256];
    int len = 0;
    while ((len = stream.read(buf, 0, buf.length)) > -1) {
        out.write(buf, 0, len);
    }
    out.close();
    stream.close();

    final Runnable finish = new Runnable() {

        @Override
        public void run() {
            if (onCompletion != null) {
                Display.getInstance().callSerially(onCompletion);

                // makes sure the file is only deleted after the onCompletion was invoked
                Display.getInstance().callSerially(new Runnable() {
                    @Override
                    public void run() {
                        temp.delete();
                    }
                });
                return;
            }
            temp.delete();
        }
    };

    if (isVideo) {
        final AndroidImplementation.Video[] retVal = new AndroidImplementation.Video[1];
        final boolean[] flag = new boolean[1];

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                VideoView v = new VideoView(getActivity());
                v.setZOrderMediaOverlay(true);
                v.setVideoURI(Uri.fromFile(temp));
                retVal[0] = new AndroidImplementation.Video(v, getActivity(), finish);
                flag[0] = true;
                synchronized (flag) {
                    flag.notify();
                }
            }
        });
        while (!flag[0]) {
            synchronized (flag) {
                try {
                    flag.wait(100);
                } catch (InterruptedException ex) {
                }
            }
        }

        return retVal[0];
    } else {
        return createMedia(createFileInputStream(temp), mimeType, finish);
    }

}