如何在每次迭代中进行循环等待?

时间:2019-10-02 20:03:26

标签: java android multithreading for-loop

我正在尝试在应用程序中使用线程在每个for循环之后等待,但是我遇到了问题。当播放来自URL的影片但必须执行此for循环时...

不幸的是,循环是由我在代码中添加的暂停执行的,后来影片以文本更新开始。这应该同时开始。 for循环和影片。在电影中,文字应该一个接一个地更新。

注意:我简化了ArrayList字典以使代码更易于理解。

注意2:应用程序尝试从URL打开视频文件,但它给我一条消息: W / MediaPlayer:无法打开(视频URL):java.io.FileNotFoundException:没有内容提供者:(视频URL)。

编辑:我正在为您添加整个课程代码。


public class Video extends Activity {

    private VideoView videoView;
    private TextView englishTrans1;
    private TextView polishTrans1;
    private TextView englishTrans2;
    private TextView polishTrans2;
    private TextView englishTrans3;
    private TextView polishTrans3;

    int j = 0;
    int i =0;
    public static final String TAG = "My tag";

    ArrayList<Translations> dict = new ArrayList<>();

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

        videoView = findViewById(R.id.video_view1);

        MediaController mMedia = new MediaController(this);

        mMedia.setMediaPlayer(videoView);
        mMedia.setAnchorView(videoView);
        videoView.setMediaController(mMedia);

        String path1 = (HERE IS VIDEO URL);

        Uri uri = Uri.parse(path1);

        videoView.setVideoURI(uri);
        videoView.start();

        englishTrans1 = findViewById(R.id.english_trans1);
        polishTrans1 = findViewById(R.id.polish_trans1);

        englishTrans2 = findViewById(R.id.english_trans2);
        polishTrans2 = findViewById(R.id.polish_trans2);

        englishTrans3 = findViewById(R.id.english_trans3);
        polishTrans3 = findViewById(R.id.polish_trans3);

        dict.add(new Translations("kot","cat"));
        dict.add(new Translations("pies","dog"));
        dict.add(new Translations("kawa","coffee"));
        dict.add(new Translations("herbata","tea"));
        dict.add(new Translations("kościół","church"));
        dict.add(new Translations("ślub","wedding"));

        final Handler h = new Handler() {

            @Override
            public void handleMessage(@NonNull Message msg) {

                for(Translations x : dict){

                    try {
                        synchronized (this) {
                            Thread.sleep(2000);
                        }

                    }catch (InterruptedException e){

                    }
                        switch (j) {
                            case 1: {
                                Log.d(TAG, "First word translated");
                                englishTrans1.setText(x.getEnglishWord());
                                polishTrans1.setText(x.getPolishWord());
                                break;
                            }
                            case 2: {
                                Log.d(TAG, "Second word translated");
                                englishTrans2.setText(x.getEnglishWord());
                                polishTrans2.setText(x.getPolishWord());
                                break;
                            }
                            case 3: {
                                Log.d(TAG, "Third word translated");
                                englishTrans3.setText(x.getEnglishWord());
                                polishTrans3.setText(x.getPolishWord());
                                break;
                            }

                        }
                        if (j < 3) {
                            j++;
                        } else {
                            j = 1;
                        }


                }


            }

        };

        Runnable r = new Runnable() {
            @Override
            public void run() {

                h.sendEmptyMessage(0);

            }
        };

        Thread t = new Thread(r);
        t.start();
    }
}

带有构造函数的Translations.java类。

public class Translations {

    private String polishWord;
    private String englishWord;

    public Translations(){
    }

    public Translations(String mPolishWord,String mEnglishWord){
        polishWord = mPolishWord;
        englishWord = mEnglishWord;
    }
    public String getPolishWord() {
        return polishWord;
    }

    public void setPolishWord(String polishWord) {
        this.polishWord = polishWord;
    }

    public String getEnglishWord() {
        return englishWord;
    }

    public void setEnglishWord(String englishWord) {
        this.englishWord = englishWord;
    }
}

2 个答案:

答案 0 :(得分:0)

为什么循环播放,代码片段正在执行的“字幕”已链接到视频。 如果人们擦洗电影并快进或快退怎么办,大多数字幕设置都会在屏幕上将句子与电影的时间码链接在一起,因此您需要在播放视频的时间码更改时触发字幕,然后将字幕传递给时间码,这样它将获得该部分视频的句子并将其显示在屏幕上。

答案 1 :(得分:0)

我找到了解决方案。问题是我试图在Handler中休眠线程。 JVM认为应该暂停主线程,而不是“ t”线程。我也将Thread.sleep()方法移到了run()和for循环上。我只在处理程序中保留了switch()来更改UI。现在可以正常工作。

        final Handler h = new Handler() {

            @Override
            public void handleMessage(@NonNull Message msg) {

               Translations x = dict.get(i-1);
                        switch (j) {
                            case 1: {
                                Log.d(TAG, "First word translated");
                                englishTrans1.setText(x.getEnglishWord());
                                polishTrans1.setText(x.getPolishWord());
                                break;
                            }
                            case 2: {
                                Log.d(TAG, "Second word translated");
                                englishTrans2.setText(x.getEnglishWord());
                                polishTrans2.setText(x.getPolishWord());
                                break;
                            }
                            case 3: {
                                Log.d(TAG, "Third word translated");
                                englishTrans3.setText(x.getEnglishWord());
                                polishTrans3.setText(x.getPolishWord());
                                break;
                            }

                        }

                }


            };


        Runnable r = new Runnable() {
            @Override
            public void run() {

                for(i = 0;i<dict.size();i++) {

                    try {
                        Thread.sleep(2000);

                    }catch (InterruptedException e){
                    }

                    if (j < 3) {
                        j++;
                    } else {
                        j = 1;
                    }
                    h.sendEmptyMessage(0);
                }


            }
        };

        Thread t = new Thread(r);
        t.start();



    }
}