从活动到主要活动并再次回到活动后,音高检测(TarsosDSP)不起作用

时间:2018-08-22 21:48:20

标签: android android-activity refresh pitch

所以我有一个主要活动,可以在其他活动之间进行选择。

在一个活动中,通过TarsosDSP进行了实时音高检测,当我第一次开始该活动时,它可以工作。但是,当我通过android后退按钮返回到主要活动,然后再次启动音高检测活动时,检测不会开始。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);
    note1 = (ImageView) findViewById(R.id.note1);
    note2 = (ImageView) findViewById(R.id.note2);
    note3 = (ImageView) findViewById(R.id.note3);
    note4 = (ImageView) findViewById(R.id.note4);
    note5 = (ImageView) findViewById(R.id.note5);
    pitchText=(TextView)findViewById(R.id.pitchText);
    noteText=(TextView)findViewById(R.id.noteText);
    mic();
    random();
}

...和音高检测

public void mic()
{

    AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,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() {
                    processPitch(pitchInHz);
                }
            });
        }
    };
    AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(pitchProcessor);
    Thread audioThread = new Thread(dispatcher, "Audio Thread");
    audioThread.start();

}
public void processPitch(float pitchInHz) {

    //there are several ifs and else ifs but would make the question too long
}

0 个答案:

没有答案