how to clear surface holder when media player is finished?

后端 未结 5 861
予麋鹿
予麋鹿 2021-01-04 05:50

I made a video player with surfaceview and mediaplayer. i have 10 videos and 10 buttons. if click on each buttons, each videos are playing.

here is my code..

相关标签:
5条回答
  • 2021-01-04 06:21

    may be you can use removeView to remove the old custome videoview ,then add the new view

    0 讨论(0)
  • 2021-01-04 06:22

    The SurfaceHolder has lockCanvas methods that will allow you to draw directly to the Surface. Use the drawColor method of the Canvas to fill it with black.

    That said, it may be preferable to remove the SurfaceView (as suggested by smile2you), because that should destroy the Surface altogether and free up unused resources. And definitely make sure you are calling release on the MediaPlayers after they are done with playback. Holding on to too many video resources will crash your app.

    0 讨论(0)
  • 2021-01-04 06:29
    video.getHolder().setFormat(PixelFormat.TRANSPARENT);
    video.getHolder().setFormat(PixelFormat.OPAQUE);
    video.setVideoURI(Uri.parse(temp));
    video.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
          mp.start();
       }
    });
    

    it work for me

    0 讨论(0)
  • 2021-01-04 06:35

    Took me two weeks to figure this out. By setting the surfaceholder to TRANSPARENT, Android will destroy the surface. Then setting it back to OPAQUE creates a new surface "clearing" the surface. Note surfacecreate and surfacedestroy events will fire, so if you have code there, beware. I put a imageview set to black to give it a black background. There maybe better ways for that.

    private void playVideoA() { 
         imageViewBlack.bringToFront();
         surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
         surfaceHolder.setFormat(PixelFormat.OPAQUE);
         surfaceView.bringToFront();
         mediaPlayerA.setDisplay(surfaceHolder);
         //surfaceView.setAlpha((float) 0.01);
         mediaPlayerA.start();
    };
    private void prepareVideoA(String url) {
         try {
            mediaPlayerA = new MediaPlayer();
            mediaPlayerA.setDataSource(url);
            mediaPlayerA.prepareAsync();
            mediaPlayerA.setOnPreparedListener(this);
            mediaPlayerA.setOnCompletionListener(this);
            mediaPlayerA.setOnBufferingUpdateListener(this);
            mediaPlayerA.setOnInfoListener(this);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    };
    @Override
    public void onPrepared(MediaPlayer mp) {
         playVideoA()
    }
    
    0 讨论(0)
  • 2021-01-04 06:36

    surfaceview.setVisibility(View.GONE);

    0 讨论(0)
提交回复
热议问题