android MediaPlayer start()函数不起作用

时间:2018-06-13 07:49:56

标签: android-mediaplayer textureview android-textureview

我正在尝试使用textureview和mediaplayer实例显示视频。我需要在加载时暂停视频。只有当用户点击屏幕时,它才会开始播放。如果用户再次点击视频暂停。

正确加载源并显示第一帧。问题是,当我点击屏幕时,功能会执行,但视频无法播放。

Logcat显示mMediaPlayer.isPlaying()甚至在mMediaPlayer.start()之后立即返回false。

这是我写的代码。

扩展FrameLayout

的类的内部构造函数
mVideoTextureView = (TextureView) findViewById(R.id.video_texture_view);
        mVideoTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
                Surface surface = new Surface(surfaceTexture);
                try{
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(getContext(), mSrc);
                    mMediaPlayer.setSurface(surface);
                    mMediaPlayer.prepare();

                    mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                        @Override
                        public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {

                        }
                    });

                    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            mediaPlayer.seekTo(mStartPosition);
                        }
                    });

                    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mPlayView.setVisibility(View.VISIBLE);

                            mDuration = mediaPlayer.getDuration();
                            setSeekBarPosition();

                            setTimeFrames();
                            setTimeVideo(0);

                            if (mOnK4LVideoListener != null) {
                                mOnK4LVideoListener.onVideoPrepared();
                            }
                        }
                    });

                    mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                        @Override
                        public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i1) {

                        }
                    });
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);



                }catch(IOException ea){
                    Log.d("DEBUG", "IOException", ea);
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

            }
        });

以下是播放和暂停视频的代码

private void onClickVideoPlayPause() {
        if(mMediaPlayer.isPlaying()){
            mPlayView.setVisibility(View.VISIBLE);
            mMessageHandler.removeMessages(SHOW_PROGRESS);
            mMediaPlayer.pause();
        } else {
            mPlayView.setVisibility(View.GONE);

            if (mResetSeekBar) {
                mResetSeekBar = false;
                mMediaPlayer.seekTo(mStartPosition);
            }

            mMessageHandler.sendEmptyMessage(SHOW_PROGRESS);
            mMediaPlayer.start();
        }
    }

0 个答案:

没有答案