2

我正在使用audiodispatcher来自 TarsosDSPlibrary 的。

pitchdetection用于检测来自麦克风的声音。一旦检测到,它就会切换到下一个活动(这是一个数学测验)。完成下一个活动的测验后,它会返回此活动并重新开始该过程。

困扰我的是,我的 APP 在使用该pitchdetection功能时 90% 的时间都在工作。但是,有时它不起作用并抛出如下错误:

E/AudioRecord: start() 状态 -38

并且应用程序不再切换到下一个活动。

package com.humanfactorsandappliedcognitionlab.research.mathsapp;

import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;

import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.RunnableFuture;

import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.io.android.AudioDispatcherFactory;
import be.tarsos.dsp.pitch.PitchDetectionHandler;
import be.tarsos.dsp.pitch.PitchDetectionResult;
import be.tarsos.dsp.pitch.PitchProcessor;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

MediaPlayer notifySound;
MediaPlayer endSound;

AudioDispatcher dispatcherMAIN;
PitchProcessor pitchProcessorMAIN;
public boolean isListening = false;

TextToSpeech tts;

private int sensitivity = 100;

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

    notifySound = MediaPlayer.create(this, R.raw.samsung);
    endSound = MediaPlayer.create(this, R.raw.ding);

    OPTION = dbHandler.getOPTION();

    tts = new TextToSpeech(this, this);
    tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
            runOnUiThread(new Runnable() {
                @Override
                public void run(){

                }
            });
        }

        @Override
        public void onDone(String utteranceId) {
            runOnUiThread(new Runnable() {
                @Override
                public void run(){

                    startListenToTalk();
                }
            });
        }

        @Override
        public void onError(String utteranceId) {

        }
    });

}

private void speakOut() {
    Log.e("TTS", "SPEAKING...");
    String text = "Please  Say  Continue  to  Proceed  ";
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}

private void startListenToTalk() {
    dispatcherMAIN = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
    pitchProcessorMAIN = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, new PitchDetectionHandler() {
        @Override
        public void handlePitch(PitchDetectionResult pitchDetectionResult,
                                AudioEvent audioEvent) {

            final float pitchInHz = pitchDetectionResult.getPitch();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    ImageButton buttonOK = (ImageButton) findViewById(R.id.buttonOK);
                    TextView textINPUT = (TextView)findViewById(R.id.textINPUT);
                    if (pitchInHz > sensitivity) {
                        Log.e("pitch : ", pitchInHz + "");
                        if (isListening) {
                            try {
                                dispatcherMAIN.stop();
                                Intent gotoMaths = new Intent(MainActivity.this, MathsActivity.class);
                                startActivity(gotoMaths);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
        }
    });

    dispatcherMAIN.addAudioProcessor(pitchProcessorMAIN);
    new Thread(dispatcherMAIN, "Audio Dispatcher").start();
    isListening = true;
}

@Override
protected void onPause() {
    super.onPause();

    if (notifySound != null) {
        notifySound.release();
    }

    if (endSound != null) {
        endSound.release();
    }

    if (isListening) {
        try {
            dispatcherMAIN.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        isListening = false;
    }

    finish();
}

@Override
public void onStop(){
    super.onStop();

    if (tts != null) {
        tts.shutdown();
    }
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        int result = tts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
                if(OPTION == "3") {
                    speakOut();
                }
        }
    } else {
        Log.e("TTS", "Initilization Failed!");
    }
}
4

0 回答 0