I am new to android development. I am just trying to record an audio with android studio(2.1.1) testing with 6.0.1 Marshmallow device.
public class MainActiv
try this code
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.record_btn:
recordAudio();
break;
case R.id.play_pause_btn:
playAudio();
break;
}
}
private void playAudio() {
if (mPlaying) {
record_btn.setEnabled(false);
startPlaying();
play_pause_btn.setText("Stop Playing");
mPlaying = false;
} else {
stopPlaying();
play_pause_btn.setText("Start Playing");
mPlaying = true;
record_btn.setEnabled(true);
}
}
private void startPlaying() {
mAudioPlayer = new MediaPlayer();
try {
mAudioPlayer.setDataSource(mFilename);
mAudioPlayer.prepare();
updatingPlyingProgressBar();
mAudioPlayer.start();
showProgressforPlaying();
} catch (IOException e) {
e.printStackTrace();
}
mAudioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playAudio();
}
});
}
private void updatingPlyingProgressBar() {
long maxTime = mAudioPlayer.getDuration();
int second = (int) maxTime / 1000;
playing_audio_progressBar.setMax(second * 1000);
playing_audio_progressBar.setProgress(0);
playing_audio_max_time.setText(second > 9 ? "00:" + second : "00:0" + second);
}
private void stopPlaying() {
mAudioPlayer.stop();
mAudioPlayer.release();
mPlayingTimer.cancel();
mAudioPlayer = null;
}
private void showProgressforPlaying() {
seconds = 0;
mPlayingTimer = new Timer();
mPlayingTimer.schedule(new TimerTask() {
@Override
public void run() {
if (!mPlaying) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (playing_audio_progressBar.getProgress() < mAudioPlayer.getDuration()) {
playing_audio_progressBar.setProgress(playing_audio_progressBar.getProgress() + 1000);
seconds++;
playing_audio_time.setText(seconds > 9 ? "00:" + seconds : "00:0" + seconds);
} else {
playAudio();
}
}
});
}
}
}, 1000, 1000);
}
private void recordAudio() {
if (mRecording) {
play_pause_btn.setEnabled(false);
mRecording = false;
audio_recording_progressBar.setMax(MAXTIME);
audio_recording_progressBar.setProgress(0);
startRecording();
showProgressforRecording();
record_btn.setText("Stop");
} else {
stopRecording();
record_btn.setText("Record");
mRecording = true;
mRecordingtimer.cancel();
play_pause_btn.setEnabled(true);
}
}
private void stopRecording() {
mAudioRecorder.stop();
mAudioRecorder.release();
mAudioRecorder = null;
}
private void startRecording() {
mAudioRecorder = new MediaRecorder();
mFilename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AudioRecordDemo/" + "Audio" + System.currentTimeMillis() + ".3gp";
mAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mAudioRecorder.setOutputFile(mFilename);
mAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mAudioRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mAudioRecorder.start();
}
private void showProgressforRecording() {
seconds = 0;
mRecordingtimer = new Timer();
mRecordingtimer.schedule(new TimerTask() {
@Override
public void run() {
if (!mRecording) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (audio_recording_progressBar.getProgress() < MAXTIME) {
audio_recording_progressBar.setProgress(audio_recording_progressBar.getProgress() + 1000);
seconds++;
recorded_audio_time.setText(seconds > 9 ? "00:" + seconds : "00:0" + seconds);
} else {
recordAudio();
}
}
});
}
}
}, 1000, 1000);
}
You can use this method for your problem's solution.
@RequiresApi(api = Build.VERSION_CODES.M)
public void getPermissionToRecordAudio() {
// 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
// checking the build version since Context.checkSelfPermission(...)
// 2) Always check for permission (even if permission has already been granted)
// since the user can revoke permissions at any time through Settings
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
// The permission is NOT already granted.
// Check if the user has been asked about this permission already and denied
// it. If so, we want to give more explanation about why the permission is needed.
// Fire off an async request to actually get the permission
// This will show the standard permission request dialog UI
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
RECORD_AUDIO_REQUEST_CODE);
}
}
You're missing some break
s in your case
s:
case R.id.start_button:
startRecord();
// You need to put a 'break;' here, or you'll fall through to the next case
case R.id.stop_button:
stoprecord();
// For the last case of a switch it might not matter, but it'd still be a good idea to put a 'break;' here as well
Query: getting error setaudiosource failed
Answer: follow the instruction. declare the permission in AndroidMainfest.xml, if you are working on Marshmallow and above then following the below lines.
requestPermissions(new String[]{
Manifest.permission_group.STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
} `enter code here`
I had the exact same problem and managed to fix it by asking for permission to record audio:
if (ActivityCompat.checkSelfPermission(activity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity(), new String[]{Manifest.permission.RECORD_AUDIO}, BuildDev.RECORD_AUDIO);
} else {
startRecording();
}
where
BuildDev.RECORD_AUDIO is public static final int RECORD_AUDIO = 0;
I am confused your title says java.lang.RuntimeException: setAudioSource failed
and your stack trace says java.lang.RuntimeException: stop failed.
For java.lang.RuntimeException: setAudioSource failed
you might be missing Runtime Permission.
You need to take Manifest.permission.RECORD_AUDIO
from user.
public void onRecordBtnClicked() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.RECORD_AUDIO },
10);
} else {
recordAudio();
}
}
private void recordAudio() {
//Record Audio.
}
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 10) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
recordAudio();
}else{
//User denied Permission.
}
}
}