1

我有一个视频应用程序。我需要应用程序按顺序播放视频(1、2、3、4、1、2、3、4),但是当我安装应用程序时,视频随机运行(2、3、1、4、2、1 ,3,4,2,4 等...)。

这是我的代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final VideoView videoView = (VideoView) findViewById(R.id.videoView_video);
    final Uri path1 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
            +R.raw.intro);
    final Uri path2 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
            +R.raw.intro2);
    final Uri path3 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
            +R.raw.intro3);
    final Uri path4 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
            +R.raw.intro4);

    videoView.setVideoURI(path1);
    videoView.start();

    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            siguiente1();
        }
        private void siguiente1(){
            videoView.setVideoURI(path1);
            videoView.start();
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    siguiente2();
                }
            });
        }

        private void siguiente2() {
            videoView.setVideoURI(path2);
            videoView.start();
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    siguiente3();
                }
            });
        }
        private void siguiente3(){
            videoView.setVideoURI(path3);
            videoView.start();
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    siguiente4();
                }
            });

        }
        private void siguiente4(){
            videoView.setVideoURI(path4);
            videoView.start();
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    siguiente1();
                }
            });

        }
    });

}

}

谢谢和对不起我的英语。

4

1 回答 1

1

尝试:

public class MainActivity extends Activity {

    private Uri path1 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
                +R.raw.intro);
    private Uri path2 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
                +R.raw.intro2);
    private Uri path3 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
                +R.raw.intro3);
    private Uri path4 = Uri.parse("android.resource://com.ivirtual.promotaxi/"
                +R.raw.intro4);

    private Uri uri_table[] = { path1, path2, path3, path4 };

    private VideoView videoView;

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

        videoView = (VideoView) findViewById(R.id.videoView_video);
        Video();
    }

    private OnCompletionListener mOnCompletionListener = new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Video();
        }
    }; // mOnCompletionListener

    private void Video() {
        Uri uri = 0;         // :P
        switch (signature_id) {
        case 0:
            uri = uri_table[1];
            break;
        case 1:
            ...
        case 4:
            uri = uri_table[0];
            break;
        }
    videoView = MediaPlayer.create(this, i);
    videoView.setOnCompletionListener(mOnCompletionListener);       
    videoView.start();
    }
于 2013-11-13T00:22:21.127 回答