Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a whatsapp like recording button with slide to cancel

As in whatsapp I need a recoding button and a slide to cancel and fade animation , I have searched for similar code but didn't got one. I am new to android programming any help or link could be helpful.

like image 704
Ranjith Avatar asked Feb 25 '15 05:02

Ranjith


People also ask

How do you record on WhatsApp without holding it?

Open an individual or group chat. Tap and hold the microphone , then slide up to lock hands-free recording. Start speaking. Once finished, tap stop .

How can I change the record of WhatsApp?

Open an individual or group chat. Tap and hold the microphone and start speaking. Slide up to lock hands-free recording.

How do I record my voice in Python?

Recording AudioThe python-sounddevice and pyaudio libraries provide ways to record audio with Python. python-sounddevice records to NumPy arrays and pyaudio records to bytes objects. Both of these can be stored as WAV files using the scipy and wave libraries, respectively.

How long can you voice record on WhatsApp?

The limit is depend on two things one is the storage capacity of your phone and another one is your willingness to hold the the record button. How were apart from that, the limit is 15 minutes. Is there a way to speed up voice messages on WhatsApp?


2 Answers

you can use the library that i have made RecordView

it's easy to setup and it's simulates the same behavior like WhatsApp.

Simply add the Views RecordView and RecordButton

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.devlomi.recordview.MainActivity">

<com.devlomi.record_view.RecordView
    android:id="@+id/record_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@id/record_button"
    app:slide_to_cancel_arrow="@drawable/ic_keyboard_arrow_left"
    app:slide_to_cancel_text="Slide To Cancel"
    app:slide_to_cancel_margin_right="10dp"/>

<com.devlomi.record_view.RecordButton
    android:id="@+id/record_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/bg_mic"
    android:scaleType="centerInside"
    app:src="@drawable/ic_mic_white"
    />

then in your Activity

    RecordView recordView = (RecordView) findViewById(R.id.record_view);
    RecordButton recordButton = (RecordButton) 
     findViewById(R.id.record_button);

    //IMPORTANT
    recordButton.setRecordView(recordView);

lastly you can handle the Record States

  • onStart when start Recording
  • onCancel when swiping to cancel
  • onFinish when finishes record and it returns the recorded time in millis
  • onLessThanSecond when the record time <= 1Second

    recordView.setOnRecordListener(this);
    
    
        @Override
        public void onStart() {
            //Start Recording..
            Log.d("RecordView", "onStart");
        }
    
        @Override
        public void onCancel() {
            //On Swipe To Cancel
            Log.d("RecordView", "onCancel");
    
        }
    
        @Override
        public void onFinish(long recordTime) {
            //Stop Recording..
            String time = getHumanTimeText(recordTime);
            Log.d("RecordView", "onFinish");
    
            Log.d("RecordTime", time);
        }
    
        @Override
        public void onLessThanSecond() {
            //When the record time is less than One Second
            Log.d("RecordView", "onLessThanSecond");
        }
    
like image 157
3llomi Avatar answered Sep 30 '22 17:09

3llomi


You can put a scale animation on the button and touch gestures to detect the user's movements..

Checkout the sample here..
https://github.com/varunjohn/Audio-Recording-Animation

This sample also has delete animation and lock feature similar to whatsapp..

enter image description here

Check Sample code here

imageViewAudio.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (isDeleting) {
                return true;
            }

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

                cancelOffset = (float) (imageViewAudio.getX() / 2.8);
                lockOffset = (float) (imageViewAudio.getX() / 2.5);

                if (firstX == 0) {
                    firstX = motionEvent.getRawX();
                }

                if (firstY == 0) {
                    firstY = motionEvent.getRawY();
                }

                startRecord();

            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP
                    || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {

                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    stopRecording(RecordingBehaviour.RELEASED);
                }

            } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {

                if (stopTrackingAction) {
                    return true;
                }

                UserBehaviour direction = UserBehaviour.NONE;

                float motionX = Math.abs(firstX - motionEvent.getRawX());
                float motionY = Math.abs(firstY - motionEvent.getRawY());

                if (motionX > directionOffset &&
                        motionX > directionOffset &&
                        lastX < firstX && lastY < firstY) {

                    if (motionX > motionY && lastX < firstX) {
                        direction = UserBehaviour.CANCELING;

                    } else if (motionY > motionX && lastY < firstY) {
                        direction = UserBehaviour.LOCKING;
                    }

                } else if (motionX > motionY && motionX > directionOffset && lastX < firstX) {
                    direction = UserBehaviour.CANCELING;
                } else if (motionY > motionX && motionY > directionOffset && lastY < firstY) {
                    direction = UserBehaviour.LOCKING;
                }

                if (direction == UserBehaviour.CANCELING) {
                    if (userBehaviour == UserBehaviour.NONE || motionEvent.getRawY() + imageViewAudio.getWidth() / 2 > firstY) {
                        userBehaviour = UserBehaviour.CANCELING;
                    }

                    if (userBehaviour == UserBehaviour.CANCELING) {
                        translateX(-(firstX - motionEvent.getRawX()));
                    }
                } else if (direction == UserBehaviour.LOCKING) {
                    if (userBehaviour == UserBehaviour.NONE || motionEvent.getRawX() + imageViewAudio.getWidth() / 2 > firstX) {
                        userBehaviour = UserBehaviour.LOCKING;
                    }

                    if (userBehaviour == UserBehaviour.LOCKING) {
                        translateY(-(firstY - motionEvent.getRawY()));
                    }
                }

                lastX = motionEvent.getRawX();
                lastY = motionEvent.getRawY();
            }
            view.onTouchEvent(motionEvent);
            return true;
        }
    });
like image 25
varunjohn1990 Avatar answered Sep 30 '22 18:09

varunjohn1990