How To Record Voice In Android?
I am trying to record the voice in android But it will create the .mp3 file on the path (sdcard/filename) But when i run this file it doesen't play because it doesn't record the vo
Solution 1:
Refer to the Android Audio Capture documentation for recording audio and playing back the recorded audio.
Solution 2:
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
publicclassAudioRecorder {
finalMediaRecorderrecorder=newMediaRecorder();
publicfinal String path;
publicAudioRecorder(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ path;
}
publicvoidstart()throws IOException {
Stringstate= android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
thrownewIOException("SD Card is not mounted. It is " + state
+ ".");
}
// make sure the directory we plan to store the recording in existsFiledirectory=newFile(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
thrownewIOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
publicvoidstop()throws IOException {
recorder.stop();
recorder.release();
}
publicvoidplayarcoding(String path)throws IOException {
MediaPlayermp=newMediaPlayer();
mp.setDataSource(path);
mp.prepare();
mp.start();
mp.setVolume(10, 10);
}
}
Solution 3:
if you searched google, you'll find this in their API Guides:
/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application's AndroidManifest.xml file, with something like:
*
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*
*/package com.android.audiorecordtest;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
publicclassAudioRecordTestextendsActivity
{
privatestaticfinalStringLOG_TAG="AudioRecordTest";
privatestaticStringmFileName=null;
privateRecordButtonmRecordButton=null;
privateMediaRecordermRecorder=null;
privatePlayButtonmPlayButton=null;
privateMediaPlayermPlayer=null;
privatevoidonRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
privatevoidonPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
privatevoidstartPlaying() {
mPlayer = newMediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
privatevoidstopPlaying() {
mPlayer.release();
mPlayer = null;
}
privatevoidstartRecording() {
mRecorder = newMediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
privatevoidstopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
classRecordButtonextendsButton {
booleanmStartRecording=true;
OnClickListenerclicker=newOnClickListener() {
publicvoidonClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
publicRecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
classPlayButtonextendsButton {
booleanmStartPlaying=true;
OnClickListenerclicker=newOnClickListener() {
publicvoidonClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
publicPlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
publicAudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@OverridepublicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayoutll=newLinearLayout(this);
mRecordButton = newRecordButton(this);
ll.addView(mRecordButton,
newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = newPlayButton(this);
ll.addView(mPlayButton,
newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
@OverridepublicvoidonPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
Solution 4:
Offical Example link from Googlehttps://developer.android.com/guide/topics/media/mediarecorder.html#example
I suggest to only follow that one since its guaranteed to be up-to-date and secure.
Post a Comment for "How To Record Voice In Android?"