Record audio like whatsapp

0

Does anyone know how to record an audio while the button is pressed and stop when you release the button like Whatsapp?

    
asked by anonymous 01.07.2017 / 05:55

1 answer

5

You can use the setOnTouchListener button to toggle the ringtone . MotionEvent.ACTION_DOWN is the event that captures the moment you clicked the button. MotionEvent.ACTION_UP is when you remove your finger from the button. Here's an example below:

class MyActivity extends Activity {

     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.your_layout);

         Button myBtn = (Button) findViewById(R.id.mybutton);
         myBtn.setOnTouchListener(new View.OnTouchListener() {
             @Override
             public boolean onTouch(View v, MotionEvent event) {
                 switch (event.getAction()) {
                     case MotionEvent.ACTION_DOWN:
                         // aqui código de iniciar a gravação que é o momento
                         // em que pressionou o botão para baixo
                         return true; 

                     case MotionEvent.ACTION_UP:
                         // aqui código de pausar a gravação que é o momento
                         // em que soltou o botão
                         return true; 
                 }
                 return false;
             }
         });
     }

 }
    
01.07.2017 / 14:01