当前时间与用户设定的时间相匹配时播放音乐

时间:2017-02-25 17:23:14

标签: android

我正在尝试创建一个类似于闹钟的应用程序,除了这个更简单并且将播放完整的歌曲曲目而不是短暂的嘈杂声音。我能够获得每秒更新的当前时间,并且我要求用户通过一系列旋转器为他们希望音乐开始的小时,分​​钟,秒和时段设置时间播放。但是,我无法使其发挥作用。

这是我的尝试:

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

    this.arraySpinner = new String[] {
            "Song 1", "Song 2", "Song 3", "Song 4", "Song 5"
    };

    this.hrSpin = new String[] {
            "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
    };

    this.minSpin = new String[] {
            "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
            "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
            "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
            "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"
    };

    this.secSpin = new String[] {
            "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
            "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
            "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
            "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"
    };

    this.perSpin = new String[] {
            "AM", "PM"
    };
    s = (Spinner) findViewById(R.id.songPicker);
    hr = (Spinner) findViewById(R.id.hrSpinner);
    min = (Spinner) findViewById(R.id.minSpinner);
    sec = (Spinner) findViewById(R.id.secSpinner);
    period = (Spinner) findViewById(R.id.periodSpinner);
    submit = (Button)findViewById(R.id.subBtn);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, arraySpinner);
    s.setAdapter(adapter);

    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, hrSpin);
    hr.setAdapter(adapter1);

    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, minSpin);
    min.setAdapter(adapter2);

    ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, secSpin);
    sec.setAdapter(adapter3);

    ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, perSpin);
    period.setAdapter(adapter4);


    mediaPlayer = MediaPlayer.create(this, R.raw.test);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            df = new SimpleDateFormat("hh:mm:ss a");
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        while (!isInterrupted()) {
                            Thread.sleep(1000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    // update TextView here!
                                    current_date = df.format(Calendar.getInstance().getTime());
                                    Toast.makeText(TimeSetter.this, "curr: "+current_date,Toast.LENGTH_LONG).show();

                                }
                            });
                        }
                    } catch (InterruptedException e) {
                    }
                }
            };
            t.start();
            new_date = hr.getSelectedItem().toString()+":"+min.getSelectedItem().toString()+":"+sec.getSelectedItem().toString()
                    + " " + period.getSelectedItem().toString();
            if(current_date.equals(new_date)) {
                mediaPlayer.start();
            }
        }
    });

}

我认为问题是onClick方法上的代码定位。您对如何实现我的目标有任何建议吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

尝试,

 SimpleDateFormat inputParser = new SimpleDateFormat(df, Locale.ENGLISH);

new_date = parseDate(hr.getSelectedItem().toString()+":"+min.getSelectedItem().toString()+":"+sec.getSelectedItem().toString()
                            + " " + period.getSelectedItem().toString();

    currentDate= parseDate(current_date);
    if(currentDate.equals(new_date);