0

我有 3 部电影附加到 aVideoView和 3 个按钮backforwardhome

我想在循环中使用back-按钮滚动浏览它们。 当我点击键到达最后一部电影时,我希望第一部电影重新开始,并且与键相同,当我到达第一部电影时,最后一个开始。 forward
forwardback

我知道这可能是一个简单的问题,但我是 android 新手,我就是想不通。

非常感谢!!!

这是我的主要活动代码:

public class Blue extends Activity implements OnClickListener{

ImageButton back;
ImageButton home;
ImageButton forward;
VideoView vid;
int count_back=2;
int count_fw=1;
String urlpath1;
String urlpath2;
String urlpath3;


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

    AdView adView = (AdView)this.findViewById(R.id.adView);
    adView.loadAd(new AdRequest());

    back = (ImageButton) findViewById(R.id.back);
    back.setOnClickListener(this);
    home = (ImageButton) findViewById(R.id.home);
    home.setOnClickListener(this);
    forward = (ImageButton) findViewById(R.id.forward);
    forward.setOnClickListener(this);


    vid = (VideoView) findViewById(R.id.videoView1);

    urlpath1 = "android.resource://"+ getPackageName()+"/"+R.raw.blue_01;
    urlpath2 = "android.resource://"+ getPackageName()+"/"+R.raw.blue_02;
    urlpath3 = "android.resource://"+ getPackageName()+"/"+R.raw.blue_03;

    vid.setVideoPath(urlpath1);
    vid.requestFocus();
    vid.start();
}

        @Override
        public void onClick(View v) {

            switch(v.getId()){

            case R.id.home:
                vid.stopPlayback();
                Intent i = new Intent(Blue.this,MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                break;

            case R.id.forward:
                if(count_fw==0){vid.setVideoPath(urlpath1);count_fw++;count_back=2;vid.requestFocus();
                vid.start();}
                else if(count_fw==1){vid.setVideoPath(urlpath2);count_fw++;count_back=0;vid.requestFocus();
                vid.start();}
                else if(count_fw==2){vid.setVideoPath(urlpath3);count_fw=0;count_back=1;vid.requestFocus();
                vid.start();}
                break;

            case R.id.back:
                if(count_back==0){vid.setVideoPath(urlpath1);count_back=2;count_fw=1;vid.requestFocus();
                vid.start();}
                else if(count_back==1){vid.setVideoPath(urlpath2);count_back--;count_fw=2;
                vid.requestFocus();vid.start();}
                else if(count_back==2){vid.setVideoPath(urlpath3);count_back--;count_fw=0;
                vid.requestFocus();vid.start();}
                break;
        }
}

@Override
public void onDestroy(){
        vid.stopPlayback();
      super.onDestroy();

}

}

4

1 回答 1

0

尝试这个

 public class Blue extends Activity {

ImageButton back;
ImageButton home;
ImageButton forward;
VideoView vid;
int count_back;
int count_fw;


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

    back = (ImageButton) findViewById(R.id.back);
    home = (ImageButton) findViewById(R.id.home);
    forward = (ImageButton) findViewById(R.id.forward);

    vid = (VideoView) findViewById(R.id.videoView1);

    final Uri uri1 = Uri.parse("android.resource://"+ getPackageName()+"/"+R.raw.blue_01);
    final Uri uri2 = Uri.parse("android.resource://"+ getPackageName()+"/"+R.raw.blue_02);
    final Uri uri3 = Uri.parse("android.resource://"+ getPackageName()+"/"+R.raw.blue_03);

    vid.setVideoURI(uri1);
    vid.requestFocus();
    vid.start();
    count_back=2;
    count_fw=1;

    home.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            vid.stopPlayback();
            Intent i = new Intent(Blue.this,MainActivity.class);
            startActivity(i);
        }
    });



    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            if(count_back==0){
           vid.setVideoURI(uri1);
           vid.requestFocus();
           vid.start();
           count_back=2;
           count_fw=1;
            }
           else if(count_back==1){
           vid.setVideoURI(uri2);
           vid.requestFocus();
           vid.start();
           count_back--;
           count_fw=2;
            }
           else if(count_back==2){
           vid.setVideoURI(uri3);
           vid.requestFocus();
           vid.start();
           count_back--;
           count_fw=0;
            }


        }
    });



    forward.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            if(count_fw==0){
            vid.setVideoURI(uri1);
            vid.requestFocus();
            vid.start();
            count_fw++;
            count_back=2;
            }
            else if(count_fw==1){
            vid.setVideoURI(uri2);
            vid.requestFocus();
            vid.start();
            count_fw++;
            count_back=0;
            }
            else if(count_fw==2){
            vid.setVideoURI(uri3);
            vid.requestFocus();
            vid.start();
            count_fw=0;
            count_back=1;
            }


        }
    });


}

    @Override
    public void onDestroy(){
        if (vid != null)
            vid.stopPlayback();
          super.onDestroy();

}

}
于 2013-11-09T18:59:32.003 回答