Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio recording issue #19

Closed
rameshpaladugu opened this issue Jul 11, 2018 · 4 comments
Closed

Audio recording issue #19

rameshpaladugu opened this issue Jul 11, 2018 · 4 comments

Comments

@rameshpaladugu
Copy link

When I click on the mic icon without holding it and next time when I try to record by holding its not getting recorded.

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

@3llomi
Copy link
Owner

3llomi commented Jul 11, 2018

@rameshpaladugu can you post your full code? i think the problem is in you App Logic.

@rameshpaladugu
Copy link
Author

rameshpaladugu commented Jul 16, 2018

    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
Copy link
Author

Error : stop called in an invalid state: 0

@3llomi
Copy link
Owner

3llomi commented Jul 18, 2018

@rameshpaladugu i am sorry, but i think this error is from MediaRecorder or MediaPlayer not from RecordView.

@3llomi 3llomi closed this as completed Jul 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants