0

Actually I want to do speech to text continuously in the background so actually I am using the solution which is given in the below link but i m not getting how can i implement it. link

I have done this so far but still my service is not running and i am not getting any output So I think that i have done something wrong in its implementation.Please help me and correct my code in order to run service and get my output

This is my mainactivity.java

public class MainActivity extends AppCompatActivity {

private int mBindFlag ;
private Messenger mServiceMessenger ;

private Button btStartService;
private TextView tvText;

@Override
protected void onStart() {
    super.onStart();
    Log.d("check", "onStart: ");

    bindService(new Intent(getApplicationContext(), MyService.class), mServiceConnection, mBindFlag);
}

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

    if (mServiceMessenger != null) {
        unbindService(mServiceConnection);
        mServiceMessenger = null;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent service = new Intent(this, MyService.class);
    this.startService(service);
    mBindFlag =  Context.BIND_ABOVE_CLIENT;
    Log.d("check", "onCreate: "+mBindFlag);
    btStartService = (Button) findViewById(R.id.btStartService);
    tvText = (TextView) findViewById(R.id.tvText);
    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {

            Manifest.permission.RECORD_AUDIO

    };

    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

}

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}








private final ServiceConnection mServiceConnection = new ServiceConnection()
{

@Override
    public void onServiceConnected(ComponentName name, IBinder service)
    {
        if (DEBUG) {
            Log.d("check", "onServiceConnected");} //$NON-NLS-1$

        mServiceMessenger = new Messenger(service);
        Message msg = new Message();
        msg.what = MyService.MSG_RECOGNIZER_START_LISTENING;

        try
        {
            mServiceMessenger.send(msg);
        }
        catch (RemoteException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name)
    {
        if (DEBUG) {Log.d("check", "onServiceDisconnected");} //$NON-NLS-1$
        mServiceMessenger = null;
    }

};

}

This is the code for my background service

public class MyService extends Service
{
    protected AudioManager mAudioManager;
    protected SpeechRecognizer mSpeechRecognizer;
    protected Intent mSpeechRecognizerIntent;
    protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));
protected boolean mIsListening;
protected volatile boolean mIsCountDownOn;

static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;

@Override
public void onCreate()
{
    super.onCreate();
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
    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());
    Log.d("check", "onCreate: "+"service");
}

protected static class IncomingHandler extends Handler
{
    private WeakReference<MyService> mtarget;

    IncomingHandler(MyService target)
    {
        mtarget = new WeakReference<MyService>(target);
    }


    @Override
    public void handleMessage(Message msg)
    {
        final MyService target = mtarget.get();

        switch (msg.what)
        {
            case MSG_RECOGNIZER_START_LISTENING:

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                {
                    // turn off beep sound
                    target.mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
                }
                if (!target.mIsListening)
                {
                    target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
                    target.mIsListening = true;
                    Log.d("check", "message start listening"); //$NON-NLS-1$
                }
                break;

            case MSG_RECOGNIZER_CANCEL:
                target.mSpeechRecognizer.cancel();
                target.mIsListening = false;
                Log.d("check", "message canceled recognizer"); //$NON-NLS-1$
                break;
        }
    }
}

// Count down timer for Jelly Bean work around
protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{

    @Override
    public void onTick(long millisUntilFinished)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish()
    {
        mIsCountDownOn = false;
        Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);
        try
        {
            mServerMessenger.send(message);
            message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
            mServerMessenger.send(message);
        }
        catch (RemoteException e)
        {

        }
    }
};

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

    if (mIsCountDownOn)
    {
        mNoSpeechCountDown.cancel();
    }
    if (mSpeechRecognizer != null)
    {
        mSpeechRecognizer.destroy();
    }
}

protected class SpeechRecognitionListener implements RecognitionListener
{

    private static final String TAG = "check";

    @Override
    public void onBeginningOfSpeech()
    {
        // speech input will be processed, so there is no need for count down anymore
        if (mIsCountDownOn)
        {
            mIsCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }
        //Log.d(TAG, "onBeginingOfSpeech"); //$NON-NLS-1$
    }

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

    }

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

    @Override
    public void onError(int error)
    {
        if (mIsCountDownOn)
        {
            mIsCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }
        mIsListening = false;
        Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
        try
        {
            mServerMessenger.send(message);
        }
        catch (RemoteException e)
        {

        }
        //Log.d(TAG, "error = " + error); //$NON-NLS-1$
    }

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

    }

    @Override
    public void onPartialResults(Bundle partialResults)
    {

    }

    @Override
    public void onReadyForSpeech(Bundle params)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            mIsCountDownOn = true;
            mNoSpeechCountDown.start();
            mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
        }
        Log.d("check", "onReadyForSpeech");
    }

    @Override
    public void onResults(Bundle results)
    {
        Toast.makeText(getApplicationContext(),results.toString(),Toast.LENGTH_LONG).show();
    }

    @Override
    public void onRmsChanged(float rmsdB)
    {

    }

}

@Override
public IBinder onBind(Intent arg0) {
    return mServerMessenger.getBinder();
}

}

Saswata
  • 1,290
  • 2
  • 14
  • 28

1 Answers1

0

I got my answer I have not enabled the service from the manifest