jaydeepw / audio-wife

A simple themable & integrable audio player library for Android.
MIT License
237 stars 78 forks source link

When tapped or clicked on the mic icon once called onLessThanSecond() and then trying to record next time not working until app is killed and opened . Getting error at stop(); Below is my implementation code #23

Open rameshpaladugu opened 6 years ago

rameshpaladugu commented 6 years ago

private void showAudioRecordPopUp() {

    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(CreateNewReminderActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.pop_up_record_audio, null);

    builder.setView(dialogView);
    final android.support.v7.app.AlertDialog dialog = builder.create();
    ImageView cancel = (ImageView) dialogView.findViewById(R.id.declineButton);

    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dismiss the alert dialog
            dialog.cancel();
            // Set the main layout background color red

        }
    });

    RecordView recordView = (RecordView) dialogView.findViewById(R.id.record_view);
    final RecordButton recordButton = (RecordButton) dialogView.findViewById(R.id.record_button);
    Button btnChangeOnclick = (Button) dialogView.findViewById(R.id.btn_change_onclick);

    //IMPORTANT
    recordButton.setRecordView(recordView);

    // if you want to click the button (in case if you want to make the record button a Send Button for example..)

// recordButton.setListenForRecord(false);

    btnChangeOnclick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (recordButton.isListenForRecord()) {
                recordButton.setListenForRecord(false);

// Toast.makeText(CreateNewReminderActivity.this, "onClickEnabled", Toast.LENGTH_SHORT).show(); } else { recordButton.setListenForRecord(true); // Toast.makeText(CreateNewReminderActivity.this, "onClickDisabled", Toast.LENGTH_SHORT).show(); } } });

    //ListenForRecord must be false ,otherwise onClick will not be called
    recordButton.setOnRecordClickListener(new OnRecordClickListener() {
        @Override
        public void onClick(View v) {

// Toast.makeText(CreateNewReminderActivity.this, "Hold on the mic to record audio", Toast.LENGTH_SHORT).show(); // dialog.dismiss(); Log.d("RecordButton" + " RECORD BUTTON CLICKED"); } });

    //Cancel Bounds is when the Slide To Cancel text gets before the timer . default is 25
    recordView.setCancelBounds(30);

    recordView.setSmallMicColor(Color.parseColor("#c2185b"));

    //prevent recording under one Second
    recordView.setLessThanSecondAllowed(false);

    recordView.setSlideToCancelText("Slide To Cancel");

    recordView.setCustomSounds(R.raw.record_start, R.raw.record_finished, 0);

    recordView.setOnRecordListener(new OnRecordListener() {
        @Override
        public void onStart() {
            Log.d("RecordView" + "  onStart");

// Toast.makeText(CreateNewReminderActivity.this, "OnStartRecord", Toast.LENGTH_SHORT).show();

            recorder = new MediaRecorder();

            //Start Recording..
            try {
                start();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onCancel() {

// Toast.makeText(CreateNewReminderActivity.this, "onCancel", Toast.LENGTH_SHORT).show();

            Log.d("RecordView" + "   onCancel");

        }

        @Override
        public void onFinish(long recordTime) {

            String time = getHumanTimeText(recordTime);

// Toast.makeText(CreateNewReminderActivity.this, "onFinishRecord - Recorded Time is: " + time, Toast.LENGTH_SHORT).show(); Log.d("RecordView" + " onFinish"); Log.d("RecordTime "+ time);

            //Stop Recording..
            try {
                stop();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }

            dialog.dismiss();
        }

        @Override
        public void onLessThanSecond() {
            Toast.makeText(CreateNewReminderActivity.this, "Press and hold to record", Toast.LENGTH_SHORT).show();
            Log.d("RecordView" + "   onLessThanSecond");

        }
    });

    recordView.setOnBasketAnimationEndListener(new OnBasketAnimationEnd() {
        @Override
        public void onAnimationEnd() {
            Log.d("RecordView" + "  Basket Animation Finished");
        }
    });

    dialog.show();
}

private String getHumanTimeText(long milliseconds) {
    return String.format("%02d:%02d",
            TimeUnit.MILLISECONDS.toMinutes(milliseconds),
            TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
}

public void start() throws IOException, IllegalStateException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String currentDateandTime = sdf.format(new Date());
    audioFileName = "Audio"+ currentDateandTime +".3gpp";
    String path = Util.getSentDirectoryPath() + File.separator + audioFileName;

    fileTemp = new File(Util.getSentDirectoryPath()  + File.separator + audioFileName);

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    recorder.start();
}

public void stop() throws IOException, IllegalStateException {

    if (recorder != null) {
        recorder.stop();
        recorder.release();
    }

    attachment_audio_section.removeAllViews();
    audio_selected_section.setVisibility(View.VISIBLE);
    isAudioFileSelected = true;
    txt_audio_file_name.setText(audioFileName);

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

            if (mAudioWife != null) {
                // when done playing, release the resources
                mAudioWife.release();

                Log.d("@@@ AudioWife release player...");
            }

            File file = new File(Util.getSentDirectoryPath()  + File.separator + audioFileName);
            if (file.exists()) {
                file.delete();

                audioFileName = null;
                isAudioFileSelected = false;
                audio_selected_section.setVisibility(View.GONE);

                attachment_audio_section.removeAllViews();
            }

            return false;
        }
    });

    Uri mUri = Uri.fromFile(new File(Util.getSentDirectoryPath() + File.separator + audioFileName));

    mAudioWife = AudioWife.getInstance();
    mAudioWife.init(getApplicationContext(), mUri).useDefaultUi(attachment_audio_section, getLayoutInflater());

}

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

    if (mAudioWife != null) {
        // when done playing, release the resources
        mAudioWife.release();

        Log.d("@@@ AudioWife release player...");
    }

}
rameshpaladugu commented 6 years ago

Error : stop called in an invalid state: 0