android-studiofirebase-realtime-databasetext-to-speechandroid-togglebutton

Toggle TextToSpeech on and off using a togglebutton?


So i have an app connected to firebase. The app retrieves data(a string) from firebase into a text view and then the TextToSpeech feature reads the text out(not from the textView but it itself retrieves the same data from firebase real-time database). I have used onDataChange() meathod so the texttospeech feature reads the text out as soon as any change in the data occurs on firebase, and not on the click of any button. Now i want to place a toggle button in my app and use it to either turn the tts feature on or off but i cant get it to work. When the toggle button is off, i want the texttospeech feature to stop and when the button state is on, i want the texttospeech feature to turn back on.This is what ive tried:

  mtts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR){
                mtts.setLanguage(Locale.US);    }else{


                Toast.makeText(MainActivity.this, "Couldnt initialize speech function!", Toast.LENGTH_SHORT).show();
            }

        }
    }); mIvToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked){
                            reff = FirebaseDatabase.getInstance().getReference().child("Match").child("Screen1");
                            reff.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                                    String tts= dataSnapshot.getValue().toString();
                                    mtts.speak(tts, TextToSpeech.QUEUE_FLUSH, null);


                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {

                                }
                            });


                        }else{
                            mtts.stop();
                            mtts.shutdown();



                        }
                    }
                });

TIA !


Solution

  • When you attach a valueEventListener the listener will trigger as long as there is change in the data, even though you checked the toggle to off. So the thing that you should do is to remove the listener when you are done:

    Directly under this:

      mtts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR){
                    mtts.setLanguage(Locale.US); 
    
                }else{
    Toast.makeText(MainActivity.this, "Couldnt initialize speech function!", Toast.LENGTH_SHORT).show();
                }
    
            }
        });
    

    Add these:

        //the reference
    
    reff=FirebaseDatabase.getInstance().getReference().child("Match").child("Screen1");
    
    
        //make a listener
    
        ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
    
         String tts= dataSnapshot.getValue().toString();
         mtts.speak(tts, TextToSpeech.QUEUE_FLUSH, null);
    
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
    
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
    
        }
    };
    
    
    
    
    
    //toggle
    
    mIvToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
    //add listener
    reff.addValueEventListener(listener);
    
    
    }else{
    //remove listener
    reff.removeEventListener(listener);
    
    mtts.stop();
    mtts.shutdown();
    
    
    
    }
    }
    });