当我按下后退按钮时,在VideoView中关闭视频

时间:2016-11-23 14:06:50

标签: android video gridview android-videoview

当我按后退按钮并播放视频时,我想要关闭VideoView。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    public Integer[] mVideo = {
            R.raw.clesella, R.raw.simonino,
            R.raw.ennio, R.raw.zip,
            R.raw.pippo, R.raw.monituri
    };

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new VideoAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               final VideoView mVideoView2 = (VideoView) findViewById(R.id.video);
                String uriPath = "android.resource://"+ getPackageName()+"/"+mVideo[position];
                Uri uri2 = Uri.parse(uriPath);
                mVideoView2.setVideoURI(uri2);
                mVideoView2.requestFocus();
                mVideoView2.setVisibility(view.VISIBLE);
                gridview.setVisibility(view.GONE);

                mVideoView2.start();

                mVideoView2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub

                        //write your code after complete video play
                        mVideoView2.setVisibility(View.GONE);
                        gridview.setVisibility(View.VISIBLE);
                    }
                });

                mVideoView2.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent motionEvent) {
                        if (mVideoView2.isPlaying()) {
                            mVideoView2.pause();
                        return  false;
                        }
                        else {
                            mVideoView2.start();
                            return false;
                        }
                    }
                });

            }
        });
    }
}

如您所见,我从原始文件夹中获取视频,然后填写GridView。当我点按该项目时,视频开始播放。我想像在setOnCompletetionListener中那样使VideoView GONE和GridView可见,但是当视频正在播放时我按下后退按钮。

2 个答案:

答案 0 :(得分:1)

将您的mVideoView2gridview全局设为您所在的班级,并且您不必在每次点击时创建匿名new MediaPlayer.OnCompletionListener(),只需创建一个全局监听器并应用它即可。

class XY extends ..{
    GridView gridview;
    VideoView mVideoView2 


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       gridview = (GridView) findViewById(R.id.gridview);
       gridview.setAdapter(new VideoAdapter(this));
       mVideoView2 = (VideoView) findViewById(R.id.video);
        mVideoView2.setOnCompletionListener(listner);

       gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          // other code to play and listen
           }
    }

    @Override
    public void onBackPressed() {
        if(videoView.isPlaying()){ // check if video is playing then hide views
            mVideoView2.setVisibility(View.GONE);
            gridview.setVisibility(View.VISIBLE);
          }else{  
               super.onBackPressed();
          }
      }

      MediaPlayer.OnCompletionListener listner = 
                        new new MediaPlayer.OnCompletionListener(){
      //... add the code here 
     } 
}

答案 1 :(得分:0)

@Override
public void onBackPressed() {

    if (mVideoView2.isPlaying()) {
        mVideoView2.setVisibility(View.GONE);
        gridview.setVisibility(View.VISIBLE);
    } else {
        super.onBackPressed();
    }
}