Search in sources :

Example 1 with Metadata

use of com.android.gallery3d.common.ApiHelper.Metadata 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)

Aggregations

AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Resources (android.content.res.Resources)1 MediaPlayer (android.media.MediaPlayer)1 OnBufferingUpdateListener (android.media.MediaPlayer.OnBufferingUpdateListener)1 OnErrorListener (android.media.MediaPlayer.OnErrorListener)1 OnVideoSizeChangedListener (android.media.MediaPlayer.OnVideoSizeChangedListener)1 SurfaceHolder (android.view.SurfaceHolder)1 Metadata (com.android.gallery3d.common.ApiHelper.Metadata)1