如何在用户说话时录制语音,以及在用户停止讲话时如何播放录制的语音?

时间:2014-02-17 08:49:19

标签: android speech-recognition voice-detection android-speech-api

我在服务类中编写SpeechRecognizer类来识别语音,并在后台连续录制和播放,但此代码无效。

任何人都可以给我一个想法我该怎么做?

这是我的Activity类:

public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
//private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
// private PlayButton   mPlayButton = null;
private MediaPlayer mPlayer = null;
Button record,play;
public static ImageView imageview;
boolean mStartRecording = false;
boolean mStartPlaying = false;
SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.activity_main);
    imageview=(ImageView)findViewById(R.id.imageview);
    Intent serviceIntent = new Intent(this, MyService.class);
    startService(serviceIntent);
}}

这是我的服务类:

 public class MyService extends Service{
private static final String LOG_TAG = null;
boolean mStartRecording;
boolean mStartPlaying;
SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
private MediaRecorder mRecorder = null;
private static String mFileName = null;
private MediaPlayer mPlayer = null;
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                                     RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                                     this.getPackageName());
    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.m4a";
}
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
protected class SpeechRecognitionListener implements RecognitionListener
   {

       private final String TAG = null;

    @Override
       public void onBeginningOfSpeech()
       {               
           //Log.d(TAG, "onBeginingOfSpeech"); 
            //Toast.makeText(getApplicationContext(), "Test", 5000).show();
            mStartRecording=true;
            AudioRecordTest.imageview.setImageResource(R.drawable.hearing);
            onRecord(mStartRecording);
       }

       @Override
       public void onBufferReceived(byte[] buffer)
       {

       }

       @Override
       public void onEndOfSpeech()
       {
           //Log.d(TAG, "onEndOfSpeech");


                stopRecording();
                mStartRecording=false;

        }

       @Override
       public void onError(int error)
       {
         //Log.d(TAG, "error = " + error);
          mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
       }

       @Override
       public void onEvent(int eventType, Bundle params)
       {

       }

       @Override
       public void onPartialResults(Bundle partialResults)
       {

       }

       @Override
       public void onReadyForSpeech(Bundle params)
       {
           Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
       }

       @Override
       public void onResults(Bundle results)
       {
           //Log.d(TAG, "onResults"); //$NON-NLS-1$
           //ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
           // matches are the return values of speech recognition engine
           // Use these values for whatever you wish to do
           mStartPlaying=true;
           AudioRecordTest.imageview.setImageResource(R.drawable.seak);
           onPlay(mStartPlaying);
       }
       @Override
       public void onRmsChanged(float rmsdB)
       {

       }
   }

 private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        //mPlayer = new MediaPlayer();
        AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE); 
        SoundPool sp = new SoundPool(10,AudioManager.STREAM_MUSIC,0);
        float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int explosion = sp.load(mFileName,0);
        float volume = actualVolume / maxVolume;
        sp.play(explosion, volume, volume, 10, 0, 1.45f);

    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mRecorder.setOutputFile(mFileName);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }}

0 个答案:

没有答案