为什么媒体播放器只播放一次我的视频?

时间:2019-05-15 15:31:59

标签: android video android-videoview

我有一个android应用,可从https站点加载内容。这发生在我的FullImageActivity中,该内容获取我的内容(图片和视频),并且我看到我的内容在屏幕上显示为缩略图。当我点击图像和/或视频时,它将带我到FullImageFragment。我正在使用最新的Glide 4.9.0以全尺寸显示我的内容(顺便说一句)。

但是,当我播放视频时,它可以播放,但每个视频只能播放一次!当我点击向后箭头以返回到缩略图,然后点击视频再次播放时,就会发生这种情况!

这是我点击手机的后退按钮时在控制台日志中看到的内容:

W/MediaPlayer: mediaplayer went away with unhandled events
    mediaplayer went away with unhandled events

这是我将图像和视频加载到 FullImageFragment 中的方式:

    import ...
    public class FullImageFragment
    extends Fragment {
     //region members variables
     private SelectedItem mCurrentItem = null; //The selected item
     private VideoView mVideoView;
     private MediaController mediaController;
     private TextView mLoadingView;
     private ImageButton mPlayButton;

     private void FullImageFragment()
     {
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
            if (getArguments() != null)
                mCurrentItem = (SelectedItem) getArguments().getSerializable(AP_ITEM_ARG);

            ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.switcherimage, container, false);

            // region Initialize global views
            this.mImage = rootView.findViewById(R.id.switchimageview);
            this.mVideoView = rootView.findViewById(R.id.switcherWebView);
            this.mLoadingView = rootView.findViewById(R.id.loading_view);
            this.mPlayButton = rootView.findViewById(R.id.play_button);
            //endregion

            mLoadingView.setVisibility(View.GONE);

            Glide.with(rootView.getContext())
                    .asBitmap()
                    .load(FooData.getPreUrl(getActivity()) + mCurrentItem.getmWebSize())
                    .into(new BitmapImageViewTarget(mImage) {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            super.onResourceReady(resource, transition);
                            mImage.setImageBitmap(resource);
                            mImage.setZoom(1);
                        }
                    });

            if (mCurrentItem.getmDownloadLink().endsWith(".mp4")) {
                //region play video function
                mPlayButton.setVisibility(View.VISIBLE);
                mPlayButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setVideoPlayerView();
                    }
                });
            } 
            else 
            {
                mVideoView.setVisibility(View.INVISIBLE);
                mLoadingView.setVisibility(View.GONE);
                mPlayButton.setVisibility(View.GONE);
            }
            return rootView;
        }
     }
    private void setVideoPlayerView() {
            int SDK_INT = android.os.Build.VERSION.SDK_INT;

            if (SDK_INT > 8) {

                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                        .permitAll().build();
                StrictMode.setThreadPolicy(policy);

            }

            this.mPlayButton.setVisibility(View.GONE);
            mLoadingView.setVisibility(View.VISIBLE);
            mImage.setVisibility(View.GONE);
            mVideoView.setVisibility(View.VISIBLE);

            mVideoView.requestFocus();
            mVideoView.bringToFront();

            // enable callbacks from MediaButtons and TransportControls
            mediaController = new MediaController(getActivity());
            mediaController.setBackgroundColor(getResources().getColor(R.color.transparent));
            mediaController.setAnchorView(mVideoView);

            String url = mCurrentItem.getmDownloadLink()
                            .replace(" ", "%20")
                    .replace("\\", "/")
                    .replace("//", "/")
                    .replace(":/", "://");

            mVideoView.setVideoPath(url);
            //mVideoView.setVideoURI(Uri.parse(url));

            mVideoView.setZOrderOnTop(true);

            mVideoView.setMediaController(mediaController);
            mVideoView.setVisibility(View.VISIBLE);

            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mVideoView.setZOrderOnTop(true);
                    mLoadingView.setVisibility(View.GONE);
                    mVideoView.requestFocus();

                    mp.setLooping(true);
                    mp.start();
                }
            });

            mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    AlertDialog dialog = new AlertDialog.Builder(getContext())
                            .setTitle(R.string.video_error_title)
                            .setMessage(R.string.video_error_message)
                            .setNeutralButton(R.string.action_ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mLoadingView.setVisibility(View.GONE);
                                    mPlayButton.setVisibility(View.VISIBLE);

                                    mImage.setVisibility(View.VISIBLE);
                                }
                            })
                            .show();

                    return true;
                }
            });

            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.start();
                }
            });
        }
/**
     *
     */
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }

    /**
     *
     */
    public void onResume() {
        Log.d(TAG, "onResume() called");
        super.onResume();
    }

    /**
     *
     */
    public void onPause() {
        Log.d(TAG, "onPause() called");
        super.onPause();
        this.viewCreated = true;

        hideVideoView();
    }

    private void hideVideoView() {
        if (mVideoView.isShown() && mVideoView.isPlaying()) {
            mVideoView.stopPlayback();
            mVideoView.suspend();
        }
    }

    @Override
    public void onStop() {
        super.onStop();

        hideVideoView();
    }
  }

有人遇到这个问题吗?是否有其他程序可以阻止我的视频?如果是这样,我需要采取什么步骤才能正确播放视频?预先非常感谢。

0 个答案:

没有答案