Example usage for android.view Surface Surface

List of usage examples for android.view Surface Surface

Introduction

In this page you can find the example usage for android.view Surface Surface.

Prototype

@UnsupportedAppUsage
    private Surface(long nativeObject) 

Source Link

Usage

From source file:net.bluehack.ui.PhotoViewer.java

@SuppressLint("NewApi")
private void preparePlayer(File file, boolean playWhenReady) {
    if (parentActivity == null) {
        return;//  w w  w .  j a v  a2  s.c o m
    }
    releasePlayer();
    if (videoTextureView == null) {
        aspectRatioFrameLayout = new AspectRatioFrameLayout(parentActivity);
        aspectRatioFrameLayout.setVisibility(View.INVISIBLE);
        containerView.addView(aspectRatioFrameLayout, 0,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.CENTER));

        videoTextureView = new TextureView(parentActivity);
        videoTextureView.setOpaque(false);
        videoTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                if (videoPlayer != null) {
                    videoPlayer.setSurface(new Surface(videoTextureView.getSurfaceTexture()));
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                if (videoPlayer != null) {
                    videoPlayer.blockingClearSurface();
                }
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                if (!textureUploaded) {
                    textureUploaded = true;
                    containerView.invalidate();
                }
            }
        });
        aspectRatioFrameLayout.addView(videoTextureView,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.CENTER));
    }
    textureUploaded = false;
    videoCrossfadeStarted = false;
    videoTextureView.setAlpha(videoCrossfadeAlpha = 0.0f);
    videoPlayButton.setImageResource(R.drawable.inline_video_play);
    if (videoPlayer == null) {
        videoPlayer = new VideoPlayer(new VideoPlayer.ExtractorRendererBuilder(parentActivity,
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
                Uri.fromFile(file)));
        videoPlayer.addListener(new VideoPlayer.Listener() {
            @Override
            public void onStateChanged(boolean playWhenReady, int playbackState) {
                if (videoPlayer == null) {
                    return;
                }
                if (playbackState != VideoPlayer.STATE_ENDED && playbackState != VideoPlayer.STATE_IDLE) {
                    try {
                        parentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    } catch (Exception e) {
                        FileLog.e("tmessages", e);
                    }
                } else {
                    try {
                        parentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    } catch (Exception e) {
                        FileLog.e("tmessages", e);
                    }
                }
                if (playbackState == VideoPlayer.STATE_READY
                        && aspectRatioFrameLayout.getVisibility() != View.VISIBLE) {
                    aspectRatioFrameLayout.setVisibility(View.VISIBLE);
                }
                if (videoPlayer.getPlayerControl().isPlaying() && playbackState != VideoPlayer.STATE_ENDED) {
                    if (!isPlaying) {
                        isPlaying = true;
                        videoPlayButton.setImageResource(R.drawable.inline_video_pause);
                        AndroidUtilities.runOnUIThread(updateProgressRunnable);
                    }
                } else if (isPlaying) {
                    isPlaying = false;
                    videoPlayButton.setImageResource(R.drawable.inline_video_play);
                    AndroidUtilities.cancelRunOnUIThread(updateProgressRunnable);
                    if (playbackState == VideoPlayer.STATE_ENDED) {
                        if (!videoPlayerSeekbar.isDragging()) {
                            videoPlayerSeekbar.setProgress(0.0f);
                            videoPlayerControlFrameLayout.invalidate();
                            videoPlayer.seekTo(0);
                            videoPlayer.getPlayerControl().pause();
                        }
                    }
                }
                updateVideoPlayerTime();
            }

            @Override
            public void onError(Exception e) {
                FileLog.e("tmessages", e);
            }

            @Override
            public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees,
                    float pixelWidthHeightRatio) {
                if (aspectRatioFrameLayout != null) {
                    if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) {
                        int temp = width;
                        width = height;
                        height = temp;
                    }
                    aspectRatioFrameLayout.setAspectRatio(
                            height == 0 ? 1 : (width * pixelWidthHeightRatio) / height,
                            unappliedRotationDegrees);
                }
            }
        });
        long duration;
        if (videoPlayer != null) {
            duration = videoPlayer.getDuration();
            if (duration == ExoPlayer.UNKNOWN_TIME) {
                duration = 0;
            }
        } else {
            duration = 0;
        }
        duration /= 1000;
        int size = (int) Math.ceil(videoPlayerTime.getPaint().measureText(String.format("%02d:%02d / %02d:%02d",
                duration / 60, duration % 60, duration / 60, duration % 60)));

        playerNeedsPrepare = true;
    }
    if (playerNeedsPrepare) {
        videoPlayer.prepare();
        playerNeedsPrepare = false;
    }
    if (videoPlayerControlFrameLayout != null) {
        if (currentBotInlineResult != null && (currentBotInlineResult.type.equals("video")
                || MessageObject.isVideoDocument(currentBotInlineResult.document))) {
            bottomLayout.setVisibility(View.VISIBLE);
            bottomLayout.setTranslationY(-AndroidUtilities.dp(48));
        }
        videoPlayerControlFrameLayout.setVisibility(View.VISIBLE);
        dateTextView.setVisibility(View.GONE);
        nameTextView.setVisibility(View.GONE);
        if (allowShare) {
            shareButton.setVisibility(View.GONE);
            menuItem.showSubItem(gallery_menu_share);
        }
    }
    if (videoTextureView.getSurfaceTexture() != null) {
        videoPlayer.setSurface(new Surface(videoTextureView.getSurfaceTexture()));
    }
    videoPlayer.setPlayWhenReady(playWhenReady);
}

From source file:kr.wdream.ui.PhotoViewer.java

@SuppressLint("NewApi")
private void preparePlayer(File file, boolean playWhenReady) {
    if (parentActivity == null) {
        return;/*from   w  ww  . java2 s  .  c om*/
    }
    releasePlayer();
    if (videoTextureView == null) {
        aspectRatioFrameLayout = new AspectRatioFrameLayout(parentActivity);
        aspectRatioFrameLayout.setVisibility(View.INVISIBLE);
        containerView.addView(aspectRatioFrameLayout, 0,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.CENTER));

        videoTextureView = new TextureView(parentActivity);
        videoTextureView.setOpaque(false);
        videoTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                if (videoPlayer != null) {
                    videoPlayer.setSurface(new Surface(videoTextureView.getSurfaceTexture()));
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                if (videoPlayer != null) {
                    videoPlayer.blockingClearSurface();
                }
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                if (!textureUploaded) {
                    textureUploaded = true;
                    containerView.invalidate();
                }
            }
        });
        aspectRatioFrameLayout.addView(videoTextureView,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.CENTER));
    }
    textureUploaded = false;
    videoCrossfadeStarted = false;
    videoTextureView.setAlpha(videoCrossfadeAlpha = 0.0f);
    videoPlayButton.setImageResource(kr.wdream.storyshop.R.drawable.inline_video_play);
    if (videoPlayer == null) {
        videoPlayer = new VideoPlayer(new VideoPlayer.ExtractorRendererBuilder(parentActivity,
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
                Uri.fromFile(file)));
        videoPlayer.addListener(new VideoPlayer.Listener() {
            @Override
            public void onStateChanged(boolean playWhenReady, int playbackState) {
                if (videoPlayer == null) {
                    return;
                }
                if (playbackState != VideoPlayer.STATE_ENDED && playbackState != VideoPlayer.STATE_IDLE) {
                    try {
                        parentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    } catch (Exception e) {
                        FileLog.e("tmessages", e);
                    }
                } else {
                    try {
                        parentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    } catch (Exception e) {
                        FileLog.e("tmessages", e);
                    }
                }
                if (playbackState == VideoPlayer.STATE_READY
                        && aspectRatioFrameLayout.getVisibility() != View.VISIBLE) {
                    aspectRatioFrameLayout.setVisibility(View.VISIBLE);
                }
                if (videoPlayer.getPlayerControl().isPlaying() && playbackState != VideoPlayer.STATE_ENDED) {
                    if (!isPlaying) {
                        isPlaying = true;
                        videoPlayButton.setImageResource(kr.wdream.storyshop.R.drawable.inline_video_pause);
                        AndroidUtilities.runOnUIThread(updateProgressRunnable);
                    }
                } else if (isPlaying) {
                    isPlaying = false;
                    videoPlayButton.setImageResource(kr.wdream.storyshop.R.drawable.inline_video_play);
                    AndroidUtilities.cancelRunOnUIThread(updateProgressRunnable);
                    if (playbackState == VideoPlayer.STATE_ENDED) {
                        if (!videoPlayerSeekbar.isDragging()) {
                            videoPlayerSeekbar.setProgress(0.0f);
                            videoPlayerControlFrameLayout.invalidate();
                            videoPlayer.seekTo(0);
                            videoPlayer.getPlayerControl().pause();
                        }
                    }
                }
                updateVideoPlayerTime();
            }

            @Override
            public void onError(Exception e) {
                FileLog.e("tmessages", e);
            }

            @Override
            public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees,
                    float pixelWidthHeightRatio) {
                if (aspectRatioFrameLayout != null) {
                    if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) {
                        int temp = width;
                        width = height;
                        height = temp;
                    }
                    aspectRatioFrameLayout.setAspectRatio(
                            height == 0 ? 1 : (width * pixelWidthHeightRatio) / height,
                            unappliedRotationDegrees);
                }
            }
        });
        long duration;
        if (videoPlayer != null) {
            duration = videoPlayer.getDuration();
            if (duration == ExoPlayer.UNKNOWN_TIME) {
                duration = 0;
            }
        } else {
            duration = 0;
        }
        duration /= 1000;
        int size = (int) Math.ceil(videoPlayerTime.getPaint().measureText(String.format("%02d:%02d / %02d:%02d",
                duration / 60, duration % 60, duration / 60, duration % 60)));

        playerNeedsPrepare = true;
    }
    if (playerNeedsPrepare) {
        videoPlayer.prepare();
        playerNeedsPrepare = false;
    }
    if (videoPlayerControlFrameLayout != null) {
        if (currentBotInlineResult != null && (currentBotInlineResult.type.equals("video")
                || MessageObject.isVideoDocument(currentBotInlineResult.document))) {
            bottomLayout.setVisibility(View.VISIBLE);
            bottomLayout.setTranslationY(-AndroidUtilities.dp(48));
        }
        videoPlayerControlFrameLayout.setVisibility(View.VISIBLE);
        dateTextView.setVisibility(View.GONE);
        nameTextView.setVisibility(View.GONE);
        if (allowShare) {
            shareButton.setVisibility(View.GONE);
            menuItem.showSubItem(gallery_menu_share);
        }
    }
    if (videoTextureView.getSurfaceTexture() != null) {
        videoPlayer.setSurface(new Surface(videoTextureView.getSurfaceTexture()));
    }
    videoPlayer.setPlayWhenReady(playWhenReady);
}