0

当检查切换按钮时,我正在尝试使声音在间隔中播放。单击切换按钮后,我的应用程序不会播放任何声音并崩溃。为什么?

代码:

    int bpm;
    double timetw;
    final Button plus = (Button) findViewById(R.id.tempop);
    final Button minus = (Button) findViewById(R.id.tempom);
    final TextView curbpm = (TextView) findViewById(R.id.curbpm);
    final ToggleButton metronomepp = (ToggleButton) findViewById (R.id.metronomepp);
    final MediaPlayer metronome = MediaPlayer.create(this, R.raw.beep);
    bpm=60;
    timetw=((60/bpm)-0.19)*10000;
    curbpm.setText("" + bpm);

    Log.i("Metronome1", ""+metronomepp.isChecked());

    metronomepp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             while (metronomepp.isChecked()) {
                    metronomepp.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                             Log.i("Metronome", "InWhile");
                             if (metronome.isPlaying()) {metronome.pause();}
                                metronome.seekTo(0);
                                metronome.setOnSeekCompleteListener(null);
                                metronome.start();
                                metronome.setOnCompletionListener(null)
                        }
                    }, (long) (timetw));
                }
        }
    });

按下切换按钮后的 LogCat:

03-14 22:54:17.094: D/dalvikvm(25418): GC_CONCURRENT freed 101K, 3% free 9518K/9735K, paused 3ms+3ms
03-14 22:54:18.019: D/dalvikvm(25418): GC_CONCURRENT freed 5K, 2% free 9923K/10055K, paused 2ms+2ms
03-14 22:54:19.744: D/dalvikvm(25418): GC_CONCURRENT freed 0K, 2% free 10312K/10439K, paused 1ms+1ms
03-14 22:54:23.039: D/dalvikvm(25418): GC_CONCURRENT freed 0K, 2% free 10759K/10887K, paused 1ms+1ms
4

2 回答 2

0

我建议在这里使用计时器。使用Timer.scheduleAtFixedRate(TimerTask task, long delay, long period),您可以安排任务以间隔(周期)运行。此外,您需要一个 TimerTask。只需创建一个扩展 TimerTask 的类并实现 run() 方法来播放节拍器声音。

给你一些代码示例:

  1. 在您的活动中创建计时器:

    class MyActivity extends Activity {
        // In the variables declared for the class:
        private Timer timer = new Timer();
        private long timerTickGapMilliseconds = 1000L; // Change this to change how often the sound is played.
    
  2. 将 MetronomeTimerTask 实现为 Activity 类的子类:

    class MetronomeTimerTask extends TimerTask {
        @Override
        public void run() {
            // play the metronome sound, from your code.
        }
    }
    
  3. 启动或停止计时器 在问题中的代码中,替换 onCheckedChanged:

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (metronomepp.isChecked()) {
            timer.scheduleAtFixedRate(new MetronomeTimerTask(), 0, timerTickGapMilliSeconds);
        }
        else {
            timer.cancel();
        }
    }
    
于 2013-03-15T08:22:14.877 回答
0

您遇到的根本问题是您正在 UI 线程中执行长时间操作。onCheckedChanged 应该派生一个线程或 Async 类,它会发出滴答声,直到它停止。最好还是有一个线程来发出你从 onCheckedChanged 打开和关闭的滴答声。

@Torben-Kohlmeier 对您的问题的回答可能已经给出了实现这一目标的最佳方法,我对其进行了编辑以明确它如何适合您的代码。

于 2013-03-14T22:26:15.257 回答