使用后台任务的最佳方法是什么?

时间:2018-04-02 18:17:47

标签: android multithreading android-asynctask handler runnable

在继续我的项目之前,我想我应该用什么来在后台运行任务。

Activity必须按住按钮 - play audiorecord videorecord user voice inputmove graph(set new coordinates with each iteration)

目前我使用MediaPlayer内置ASync playing audioRunOnUiThread Runnable audio recordingHandler recording用于移动图表,我还没有获得 AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(44100,2048,0); PitchDetectionHandler pdh = new PitchDetectionHandler() { @Override public void handlePitch(PitchDetectionResult res, AudioEvent e){ final float pitchInHz = res.getPitch(); runOnUiThread(new Runnable() { @Override public void run() { noteText.setText(micInputResult.processPitch(pitchInHz)); pitchText.setText("" + pitchInHz); gameView.setSingerArrowY(micInputResult.processPlayerCoordinate(pitchInHz)); gameView.invalidate(); } }); } }; AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 44100, 2048, pdh); dispatcher.addAudioProcessor(pitchProcessor); Thread audioThread = new Thread(dispatcher, "Audio Thread"); audioThread.start(); 视频。应在每3或5毫秒后更新UI。

我的问题是在这种情况下使用的最佳选择。

这是我的录音和音高检测:

        playRecordButton.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionevent) {
            final int action = motionevent.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
                playMedia(songIndex);

                runnable = new Runnable() {
                    public void run() {

                        //Setting new coordinates for graph on each iteration
                        assa.setMinX(orderNr);
                        assa.setMaxX(orderNr+5);


                        graph.invalidate();
                        if(pitchesList.size() != orderNr){
                            orderNr++;
                        }
                        handler.postDelayed(this, 100);
                    }
                };

                handler.postDelayed(runnable, 100);
            } else if (action == MotionEvent.ACTION_UP) {
                Log.i("repeatBtn", "MotionEvent.ACTION_UP");
                handler.removeCallbacks(runnable);
                assa.setMinX(-2);
                assa.setMaxX(2);
                graph.invalidate();
                orderNr = 0;
                stopAudio();

            }//end else
            return false;
        } //end onTouch
    }); //end b my button

移动图表:

{{1}}

我知道他们最终应该马上跑,但这是问的原因,因为我尝试了很多不同的方法,但我不确定应该使用哪种方式。

0 个答案:

没有答案