Search in sources :

Example 96 with SurfaceHolder

use of android.view.SurfaceHolder in project butter-android by butterproject.

the class AbsPlayerFragment method updateSurfaceSize.

@Override
public void updateSurfaceSize(final int width, final int height) {
    SurfaceHolder holder = videoSurface.getHolder();
    holder.setFixedSize(width, height);
    ViewGroup.LayoutParams lp = videoSurface.getLayoutParams();
    lp.width = width;
    lp.height = height;
    videoSurface.setLayoutParams(lp);
}
Also used : SurfaceHolder(android.view.SurfaceHolder) ViewGroup(android.view.ViewGroup)

Example 97 with SurfaceHolder

use of android.view.SurfaceHolder in project android_packages_apps_Gallery2 by LineageOS.

the class CodeauroraVideoView method initialize.

private void initialize() {
    mPreparedListener = new MediaPlayer.OnPreparedListener() {

        public void onPrepared(final MediaPlayer mp) {
            if (LOG) {
                Log.v(TAG, "mPreparedListener.onPrepared(" + mp + ")");
            }
            // Here we can get meta data from mediaplayer.
            // Get the capabilities of the player for this stream
            final Metadata data = ApiHelper.MediaPlayer.getMetadata(mp, ApiHelper.MediaPlayer.METADATA_ALL, ApiHelper.MediaPlayer.BYPASS_METADATA_FILTER);
            if (data != null) {
                mCanPause = !data.has(Metadata.PAUSE_AVAILABLE) || data.getBoolean(Metadata.PAUSE_AVAILABLE);
                mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE) || data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);
                mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE) || data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);
                mCanSeek = !data.has(Metadata.SEEK_AVAILABLE) || data.getBoolean(Metadata.SEEK_AVAILABLE);
            } else {
                mCanPause = true;
                mCanSeekBack = true;
                mCanSeekForward = true;
                mCanSeek = true;
                Log.w(TAG, "Metadata is null!");
            }
            if (LOG) {
                Log.v(TAG, "mPreparedListener.onPrepared() mCanPause=" + mCanPause);
            }
            mHasGotPreparedCallBack = true;
            doPreparedIfReady(mMediaPlayer);
        }
    };
    mErrorListener = new MediaPlayer.OnErrorListener() {

        public boolean onError(final MediaPlayer mp, final int frameworkErr, final int implErr) {
            Log.d(TAG, "Error: " + frameworkErr + "," + implErr);
            // record error position and duration
            // here disturb the original logic
            mSeekWhenPrepared = getCurrentPosition();
            if (LOG) {
                Log.v(TAG, "onError() mSeekWhenPrepared=" + mSeekWhenPrepared + ", mDuration=" + mDuration);
            }
            // for old version Streaming server, getduration is not valid.
            mDuration = Math.abs(mDuration);
            mCurrentState = STATE_ERROR;
            mTargetState = STATE_ERROR;
            if (mMediaController != null) {
                mMediaController.hide();
            }
            /* If an error handler has been supplied, use it and finish. */
            if (mOnErrorListener != null) {
                if (mOnErrorListener.onError(mMediaPlayer, frameworkErr, implErr)) {
                    return true;
                }
            }
            mMediaPlayer.reset();
            /* Otherwise, pop up an error dialog so the user knows that
                 * something bad has happened. Only try and pop up the dialog
                 * if we're attached to a window. When we're going away and no
                 * longer have a window, don't bother showing the user an error.
                 */
            if (getWindowToken() != null && mErrorDialogShowing == false) {
                final Resources r = getContext().getResources();
                int messageId;
                if (frameworkErr == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
                    messageId = android.R.string.VideoView_error_text_invalid_progressive_playback;
                } else {
                    messageId = android.R.string.VideoView_error_text_unknown;
                }
                new AlertDialog.Builder(getContext()).setMessage(messageId).setPositiveButton(android.R.string.VideoView_error_button, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* If we get here, there is no onError listener, so
                                         * at least inform them that the video is over.
                                         */
                        mErrorDialogShowing = false;
                        if (mOnCompletionListener != null) {
                            mOnCompletionListener.onCompletion(mMediaPlayer);
                        }
                        release(true);
                    }
                }).setCancelable(false).show();
                mErrorDialogShowing = true;
            }
            return true;
        }
    };
    mBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {

        public void onBufferingUpdate(final MediaPlayer mp, final int percent) {
            mCurrentBufferPercentage = percent;
            if (mOnBufferingUpdateListener != null) {
                mOnBufferingUpdateListener.onBufferingUpdate(mp, percent);
            }
            if (LOG) {
                Log.v(TAG, "onBufferingUpdate() Buffering percent: " + percent);
                Log.v(TAG, "onBufferingUpdate() mTargetState=" + mTargetState);
                Log.v(TAG, "onBufferingUpdate() mCurrentState=" + mCurrentState);
            }
        }
    };
    mSizeChangedListener = new MediaPlayer.OnVideoSizeChangedListener() {

        public void onVideoSizeChanged(final MediaPlayer mp, final int width, final int height) {
            mVideoWidth = mp.getVideoWidth();
            mVideoHeight = mp.getVideoHeight();
            if (LOG) {
                Log.v(TAG, "OnVideoSizeChagned(" + width + "," + height + ")");
                Log.v(TAG, "OnVideoSizeChagned(" + mVideoWidth + "," + mVideoHeight + ")");
                Log.v(TAG, "OnVideoSizeChagned() mCurrentState=" + mCurrentState);
            }
            if (mVideoWidth != 0 && mVideoHeight != 0) {
                getHolder().setFixedSize(mVideoWidth, mVideoHeight);
                if (mCurrentState == STATE_PREPARING) {
                    mNeedWaitLayout = true;
                }
            }
            if (mVideoSizeListener != null) {
                mVideoSizeListener.onVideoSizeChanged(mp, width, height);
            }
            CodeauroraVideoView.this.requestLayout();
        }
    };
    getHolder().removeCallback(mSHCallback);
    mSHCallback = new SurfaceHolder.Callback() {

        public void surfaceChanged(final SurfaceHolder holder, final int format, final int w, final int h) {
            if (LOG) {
                Log.v(TAG, "surfaceChanged(" + holder + ", " + format + ", " + w + ", " + h + ")");
                Log.v(TAG, "surfaceChanged() mMediaPlayer=" + mMediaPlayer + ", mTargetState=" + mTargetState + ", mVideoWidth=" + mVideoWidth + ", mVideoHeight=" + mVideoHeight);
            }
            mSurfaceWidth = w;
            mSurfaceHeight = h;
            final boolean isValidState = (mTargetState == STATE_PLAYING);
            final boolean hasValidSize = (mVideoWidth == w && mVideoHeight == h);
            if (mMediaPlayer != null && isValidState && hasValidSize) {
                if (mSeekWhenPrepared != 0) {
                    seekTo(mSeekWhenPrepared);
                }
                Log.v(TAG, "surfaceChanged() start()");
                start();
            }
        }

        public void surfaceCreated(final SurfaceHolder holder) {
            if (LOG) {
                Log.v(TAG, "surfaceCreated(" + holder + ")");
            }
            /*
                if (mCurrentState == STATE_SUSPENDED) {
                    mSurfaceHolder = holder;
                    mMediaPlayer.setDisplay(mSurfaceHolder);
                    if (mMediaPlayer.resume()) {
                        mCurrentState = STATE_PREPARED;
                        if (mSeekWhenPrepared != 0) {
                            seekTo(mSeekWhenPrepared);
                        }
                        if (mTargetState == STATE_PLAYING) {
                            start();
                        }
                        return;
                    } else {
                        release(false);
                    }
                }
                */
            mSurfaceHolder = holder;
            openVideo();
        }

        public void surfaceDestroyed(final SurfaceHolder holder) {
            // after we return from this we can't use the surface any more
            if (LOG) {
                Log.v(TAG, "surfaceDestroyed(" + holder + ")");
            }
            mSurfaceHolder = null;
            if (mMediaController != null) {
                mMediaController.hide();
            }
            if (isHTTPStreaming(mUri) && mCurrentState == STATE_SUSPENDED) {
                // Don't call release() while run suspend operation
                return;
            }
            release(mTargetState == STATE_ERROR || mCurrentState == STATE_ERROR);
        }
    };
    getHolder().addCallback(mSHCallback);
}
Also used : AlertDialog(android.app.AlertDialog) OnErrorListener(android.media.MediaPlayer.OnErrorListener) DialogInterface(android.content.DialogInterface) Metadata(com.android.gallery3d.common.ApiHelper.Metadata) SurfaceHolder(android.view.SurfaceHolder) OnBufferingUpdateListener(android.media.MediaPlayer.OnBufferingUpdateListener) Resources(android.content.res.Resources) OnVideoSizeChangedListener(android.media.MediaPlayer.OnVideoSizeChangedListener) MediaPlayer(android.media.MediaPlayer)

Example 98 with SurfaceHolder

use of android.view.SurfaceHolder in project android_packages_apps_Snap by LineageOS.

the class DrawAutoHDR method onPreviewUIReady.

@Override
public void onPreviewUIReady() {
    if (mPaused || mCameraDevice == null) {
        return;
    }
    Log.v(TAG, "onPreviewUIReady");
    if (mCameraState == PREVIEW_STOPPED) {
        startPreview();
    } else {
        synchronized (mCameraDevice) {
            SurfaceHolder sh = mUI.getSurfaceHolder();
            if (sh == null) {
                Log.w(TAG, "startPreview: holder for preview are not ready.");
                return;
            }
            mCameraDevice.setPreviewDisplay(sh);
        }
    }
}
Also used : SurfaceHolder(android.view.SurfaceHolder)

Example 99 with SurfaceHolder

use of android.view.SurfaceHolder in project android_packages_apps_Snap by LineageOS.

the class DrawAutoHDR method startPreview.

/**
 * This can run on a background thread, so don't do UI updates here. Post any
 *             view updates to MainHandler or do it on onPreviewStarted() .
 */
private void startPreview() {
    if (mPaused || mCameraDevice == null || mParameters == null) {
        return;
    }
    synchronized (mCameraDevice) {
        SurfaceHolder sh = null;
        Log.v(TAG, "startPreview: SurfaceHolder (MDP path)");
        if (mUI != null) {
            sh = mUI.getSurfaceHolder();
        }
        // Let UI set its expected aspect ratio
        mCameraDevice.setPreviewDisplay(sh);
    }
    if (!mCameraPreviewParamsReady) {
        Log.w(TAG, "startPreview: parameters for preview are not ready.");
        return;
    }
    mErrorCallback.setActivity(mActivity);
    mCameraDevice.setErrorCallback(mErrorCallback);
    // Reset camera state after taking a picture
    if (mCameraState != PREVIEW_STOPPED && mCameraState != INIT) {
        setCameraState(IDLE);
    }
    // Preview needs to be stopped when changing resolution
    if (mRestartPreview && mCameraState != PREVIEW_STOPPED && mCameraState != INIT) {
        stopPreview();
        mRestartPreview = false;
    }
    if (mFocusManager == null)
        initializeFocusManager();
    if (!mSnapshotOnIdle) {
        // Unlock AE and AWB.
        mFocusManager.setAeAwbLock(false);
    }
    setCameraParameters(UPDATE_PARAM_ALL);
    mCameraDevice.setOneShotPreviewCallback(mHandler, new CameraManager.CameraPreviewDataCallback() {

        @Override
        public void onPreviewFrame(byte[] data, CameraProxy camera) {
            mUI.hidePreviewCover();
        }
    });
    mCameraDevice.startPreview();
    mHandler.sendEmptyMessage(ON_PREVIEW_STARTED);
    setDisplayOrientation();
    if (!mSnapshotOnIdle && !mInstantCaptureSnapShot) {
        // resume it because it may have been paused by autoFocus call.
        if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode(false)) && mCameraState != INIT || CameraUtil.FOCUS_MODE_MW_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode(false))) {
            mCameraDevice.cancelAutoFocus();
        }
    } else {
        Log.v(TAG, "Trigger snapshot from start preview.");
        mHandler.sendEmptyMessageDelayed(INSTANT_CAPTURE, 1500);
    }
}
Also used : SurfaceHolder(android.view.SurfaceHolder) CameraProxy(com.android.camera.CameraManager.CameraProxy)

Example 100 with SurfaceHolder

use of android.view.SurfaceHolder in project android_packages_apps_Snap by LineageOS.

the class VideoModule method startPreview.

private void startPreview() {
    Log.v(TAG, "startPreview");
    mStartPrevPending = true;
    SurfaceHolder sh = null;
    Log.v(TAG, "startPreview: SurfaceHolder (MDP path)");
    sh = mUI.getSurfaceHolder();
    if (!mPreferenceRead || mPaused == true || mCameraDevice == null) {
        mStartPrevPending = false;
        return;
    }
    mErrorCallback.setActivity(mActivity);
    mCameraDevice.setErrorCallback(mErrorCallback);
    if (mPreviewing == true) {
        stopPreview();
    }
    setDisplayOrientation();
    mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
    setCameraParameters(true);
    try {
        mCameraDevice.setPreviewDisplay(sh);
        mCameraDevice.setOneShotPreviewCallback(mHandler, new CameraManager.CameraPreviewDataCallback() {

            @Override
            public void onPreviewFrame(byte[] data, CameraProxy camera) {
                mUI.hidePreviewCover();
            }
        });
        mCameraDevice.startPreview();
        mPreviewing = true;
        onPreviewStarted();
    } catch (Throwable ex) {
        closeCamera();
        throw new RuntimeException("startPreview failed", ex);
    }
    mStartPrevPending = false;
    mFocusManager.onPreviewStarted();
}
Also used : SurfaceHolder(android.view.SurfaceHolder) CameraProxy(com.android.camera.CameraManager.CameraProxy)

Aggregations

SurfaceHolder (android.view.SurfaceHolder)189 SurfaceView (android.view.SurfaceView)62 LargeTest (android.test.suitebuilder.annotation.LargeTest)50 Parameters (android.hardware.Camera.Parameters)37 IOException (java.io.IOException)29 MediaPlayer (android.media.MediaPlayer)17 Surface (android.view.Surface)16 SurfaceTexture (android.graphics.SurfaceTexture)14 CamcorderProfile (android.media.CamcorderProfile)13 MediaRecorder (android.media.MediaRecorder)13 Camera (android.hardware.Camera)9 OverlayData (android.media.videoeditor.VideoEditor.OverlayData)9 Paint (android.graphics.Paint)8 AudioManager (android.media.AudioManager)8 SharedPreferences (android.content.SharedPreferences)7 MediaVideoItem (android.media.videoeditor.MediaVideoItem)7 CameraManager (com.google.zxing.client.android.camera.CameraManager)7 Intent (android.content.Intent)6 VideoEditor (android.media.videoeditor.VideoEditor)6 MediaProcessingProgressListener (android.media.videoeditor.VideoEditor.MediaProcessingProgressListener)6