Get size of audio while recording android? - android

I am working on an android application . I am recording audio using MediaRecorder class. I want to check size of recorded audio while recording and check size of recorded audio if size greater than certain MB I want stop recording . How can I achieve this . any help will be appreciated .
public class RecordAudio {
private MediaRecorder mRecorder = null;
public RecordAudio() {
}
//
public void prepareRecording(String fileName) {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
}
if (mRecorder != null) {
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(app.appExternalDir + "/" + fileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
System.out.println("this is max::::"+mRecorder.getAudioSourceMax());
try {
mRecorder.prepare();
} catch (IOException e) {
}
}
}
// This starts the recording of the voice memo
public void startRecording() {
if (mRecorder != null) {
mRecorder.start();
}
}
// This stops the recording and save the data on storage
public void stopRecording() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
}

use below code to get size of your recorded file
try{
File file = new File(path_of_your_recorded_file);
long length = file.length();
length = length/1024;
System.out.println("File Path : " + file.getPath() + ", File size : " + length +" KB");
}catch(Exception e){
System.out.println("File not found : " + e.getMessage() + e);
}
then once you get length you can chek as per your requirement hope it will solve your problem

For those who came across this now:
There is an Interface that you can implement :
public class RecordingService extends Service implements
MediaRecorder.OnInfoListener{
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Log.d(TAG_FOREGROUND_SERVICE,"inside the onInfo");
//check whether file size has reached to Max size to stop recording
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED)
{
Log.d(TAG_FOREGROUND_SERVICE,"Media exceeded the size");
}
}
}
And In your MediaRecorder initialisation code you can set the interface callback and size
eg:
mRecorder = new MediaRecorder();
mRecorder.setOnInfoListener(this);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setMaxFileSize(Audio_MAX_FILE_SIZE);
Hope it helps

Related

Screen recording in android using MediaRecorder api

Hey I'm trying to record a screencast app in android lolipop using mediarecorder api. Problem is that my application acts weird. Whenever I call start recording on the VideoRecorder class even when hardcoding the configuration like video size and output file the phone reboots. The app previosuly worked fine - saved the stuff in the correct place and the video itself looked good but then I've changed something in the code and now it doesn't work. Any idea what I'm missing?
Here is my code:
public class VideoRecorder {
private int screenDensity, screenHeight, screenWidth;
private MediaRecorder mMediaRecorder;
private VirtualDisplay mVirtualDisplay;
private MediaProjection mediaProjection;
private String directory, filename;
private Display defaultDisplay = null;
private DisplayMetrics metrics;
public VideoRecorder(Display defaultDisplay) {
//this.defaultDisplay = defaultDisplay;
//metrics = new DisplayMetrics();
//defaultDisplay.getMetrics(metrics);
//screenDensity = metrics.densityDpi;
// screenHeight = metrics.heightPixels;
// screenWidth = metrics.widthPixels;
}
void prepareVideoRecorder() {
initRecorder();
prepareRecorder();
}
private void initRecorder() {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(540, 888);
// mMediaRecorder.setOutputFile(directory + "/" + filename + ".mp4");
mMediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsoluteFile() + "/recorderrsr.mp4");
}
void startRecording(MediaProjection mediaProjection) {
mMediaRecorder = new MediaRecorder();
initRecorder();
prepareRecorder();
this.mediaProjection = mediaProjection;
mMediaRecorder.start();
mVirtualDisplay = createVirtualDisplay();
}
private VirtualDisplay createVirtualDisplay() {
return mediaProjection.createVirtualDisplay("MainActivity",
540, 888, 240,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}
void setFilename(String filename) {
this.filename = filename;
}
void setDirectory(String directory) {
this.directory = directory;
}
void stopRecording() {
mMediaRecorder.stop();
mMediaRecorder.reset();
if (mediaProjection != null) {
mediaProjection.stop();
mediaProjection = null;
}
if (mMediaRecorder != null) {
mMediaRecorder.release();
mMediaRecorder = null;
}
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
}
}
private void prepareRecorder() {
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
void release() {
mMediaRecorder.release();
}
}
I'm sure that the location exists and I have the correct permissions - as I've said the app worked fine before.
Thanks for help Jon

AudioRecord can not be initialized

I am trying to work with audiorecorder, but I am getting illegal argument exceptions stating that the audiorecorder is not initialised.
My code is like the one shown here
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
I have seen another answer which seems to work for some people but it isn't working for me
AudioRecord object not initializing
Try this instead:
private MediaRecorder mRecorder = null;
private void startRecording() {
String fileSaveName = generateNameForAudioFile();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(fileSaveName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
startRecording.setEnabled(true);
try {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
} catch (Exception e) {
}
}
public String generateNameForAudioFile() {
String audioName = GetrandFilename();
mFileName = Environment.getExternalStorageDirectory().getPath() + "/"
+ audioName + "myaudio" + ".3gp";
);
return mFileName;
}
#Override
public void onPause() {
super.onPause();
try {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
} catch (Exception e) {
}
}
Let me know if this post is of any help.
I was actually doing the silly thing I was giving the permissions inside application tag corrected it and it's working fine thanks everyone for the time and support

Android MediaRecorder - "start failed: -19"

I'm trying to create a video recorder on Android, and I've prepared my code which is supposed to be working - but I constantly get an error message start failed: -19.
Here's my code:
public boolean startRecording() {
try {
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
#Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.i(TAG, "Error");
}
});
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
Log.i(TAG, "a");
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
Log.i(TAG, "b");
mediaRecorder.setMaxDuration(maxDurationInMs); // set to 20000
String uniqueOutFile = OUTPUT_FILE + System.currentTimeMillis() + ".3gp";
File outFile = new File(uniqueOutFile);
if (outFile.exists()) {
outFile.delete();
}
mediaRecorder.setOutputFile(uniqueOutFile);
mediaRecorder.setVideoFrameRate(videoFramesPerSecond); // set to 20
mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());
Log.i(TAG, "c");
mediaRecorder.setPreviewDisplay(holder.getSurface());
mediaRecorder.setMaxFileSize(maxFileSizeInBytes); // set to 50000
mediaRecorder.prepare();
Log.i(TAG, "d");
mediaRecorder.start();
Log.i(TAG, "e");
return true;
} catch (IllegalStateException e) {
Log.i(TAG, "f");
Log.e(TAG, e.getMessage());
e.printStackTrace();
camera.lock();
return false;
} catch (IOException e) {
Log.i(TAG, "g");
Log.e(TAG, e.getMessage());
e.printStackTrace();
camera.lock();
return false;
} catch (RuntimeException e) {
Log.i(TAG, "h");
Log.e(TAG, e.getMessage());
camera.lock();
return false;
}
}
All the debug logs (from "a" through "d") are printed in log, so it seems that all the steps upto mediaRecorder.prepare() are properly done. Then it catches a RuntimeException with message start failed: -19. There is a similar question, but that doesn't solve my problem.
Is there any other reason to get such an error?
Just found out the bug, in this line:
mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());
after commenting out this line, the code runs perfectly!
I solved my problem once i added this for video recording
/**
* Start video recording by cleaning the old camera preview
*/
private void startVideoRecorder() {
// THIS IS NEEDED BECAUSE THE GLASS CURRENTLY THROWS AN ERROR OF
// "MediaRecorder start failed: -19"
// THIS WONT BE NEEDED INCASE OF PHONE AND TABLET
// This causes crash in glass kitkat version so remove it
// try {
// mCamera.setPreviewDisplay(null);
// } catch (java.io.IOException ioe) {
// Log.d(TAG,
// "IOException nullifying preview display: "
// + ioe.getMessage());
// }
// mCamera.stopPreview();
// mCamera.unlock();
recorder = new MediaRecorder();
// Let's initRecorder so we can record again
initRecorder();
}
/**
* Initialize video recorder to record video
*/
private void initRecorder() {
try {
File dir = new File(folderPath);
if (!dir.exists()) {
dir.mkdirs();
}
mCamera.stopPreview();
mCamera.unlock();
videofile = new File(dir, fileName + ".mp4");
recorder.setCamera(mCamera);
// Step 2: Set sources
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
recorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
recorder.setOutputFile(videofile.getAbsolutePath());
// Step 5: Set the preview output
recorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
recorder.setMaxDuration(video_duration * 1000);
recorder.setOnInfoListener(new OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mCamera.stopPreview();
releaseMediaRecorder();
/*
* initiate media scan and put the new things into the
* path array to make the scanner aware of the location
* and the files you want to see
*/MediaScannerConnection.scanFile(
CuxtomCamActivity.this,
new String[] { videofile.getPath() }, null,
null);
Intent intent = new Intent();
intent.putExtra(CuxtomIntent.FILE_PATH,
videofile.getPath());
intent.putExtra(CuxtomIntent.FILE_TYPE, FILE_TYPE.VIDEO);
setResult(RESULT_OK, intent);
finish();
}
}
});
recorder.prepare();
recorder.start();
} catch (Exception e) {
Log.e("Error Stating CuXtom Camera", e.getMessage());
}
}
private void releaseMediaRecorder() {
if (recorder != null) {
recorder.reset(); // clear recorder configuration
recorder.release(); // release the recorder object
recorder = null;
}
}
For detailed guide refer to this Open Source Cuxtom Cam
the problem is in your setVideoSize() code .
and why this code error ...
From the research I have done, error code -19 comes about when there is a problem with the size of the video as set by MediaRecorder#setVideoSize()
run this code , and see whitch screen that your camera in your device can support :
final List<Camera.Size> mSupportedVideoSizes = getSupportedVideoSizes(mCamera);
for (Camera.Size str : mSupportedVideoSizes)
Log.e(TAG, "mSupportedVideoSizes "+str.width + ":" + str.height + " ... "
+ ((float) str.width / str.height));
and method is :
public List<Size> getSupportedVideoSizes(Camera camera) {
if (camera.getParameters().getSupportedVideoSizes() != null) {
return camera.getParameters().getSupportedVideoSizes();
} else {
// Video sizes may be null, which indicates that all the supported
// preview sizes are supported for video recording.
return camera.getParameters().getSupportedPreviewSizes();
}
}
I had that problem with some specific phones, I've found out that I couldn't set camcoder profile sizes in some of them. But when that worked for the problematic androids it stopped working on the previous working devices.
So in the end my implemented logic was something like:
Set width/height
Try to start the merdia recorder
In case of exception, try again without setting width/height
Kind of a trash logic, but that worked.
I've setup a github project with that implementation, try it out: https://github.com/rafaelsilverio/MediaRecorder
I also encountered this problem and annotated the following two ways, because the hardware does not support the two configurations.
MediaRecorder .setVideoSize()
MediaRecorder .setVideoFrameRate()

how to save the recorded audio files in another folder programmatically?

i'm trying to save the recorded audio files in a folder that i wanted it to be rather then the default folder. but somehow i failed to do so.
my code:
Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Record/sound_"+ String.valueOf(System.currentTimeMillis()) + ".amr"));
recordIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(recordIntent, RESULT_OK);
it did calls the voice recorder app. and also when i press the stop button, it return to my app and have a toast appeared saying its saved. but, rather then saving in my Record folder, it save in the default folder.
i realized that there is error msg in the logcat :
01-29 01:34:23.900: E/ActivityThread(10824): Activity com.sec.android.app.voicerecorder.VoiceRecorderMainActivity has leaked ServiceConnection com.sec.android.app.voicerecorder.util.VRUtil$ServiceBinder#405ce7c8 that was originally bound here
i'm not sure what went wrong as the code works when i call the camera app.
Do in this way, Record with MediaRecorder:
To start Recording:
public void startRecording()
{
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try
{
recorder.prepare();
recorder.start();
}
catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
To Stop:
private void stopRecording()
{
if(null != recorder)
{
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
For Selected Folder:
private String getFilename()
{
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
i've found a way to solve this problem, even though it takes a round to do it rather then getting to the point straight, but its the best that i've got and it also work.
instead of calling the voice recorder app with extra included, i just call it without any input :
Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(recordIntent, 1111);
then, add an onActivityResult, with the request code == 1111 (depends on what you put) and retrieve the last modified file that consist of the extension "3ga" from the default folder of recorder "Sounds"
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1111)
{
File folder = new File(Environment.getExternalStorageDirectory(), "/Sounds");
long folderModi = folder.lastModified();
FilenameFilter filter = new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return (name.endsWith(3ga));
}
};
File[] folderList = folder.listFiles(filter);
String recentName = "";
for(int i=0; i<folderList.length;i++)
{
long fileModi = folderList[i].lastModified();
if(folderModi == fileModi)
{
recentName = folderList[i].getName();
}
}
}
this way, i can get the name of the file and also do the modification (e.g renaming) with it.
hope this helps other people. =)
I used this way before and it is Ok for me!
private MediaRecorder mRecorder = null;
public void startRecording() {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(getFilename());
try {
mRecorder.prepare();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mRecorder.start();
}
}
to Stop recording:
public void stopRecording() {
if (mRecorder != null) {
mRecorder.stop();
timer.cancel();
mRecorder.release();
mRecorder = null;
}
}
To save file:
#SuppressLint("SdCardPath")
private String getFilename() {
file = new File("/sdcard", "MyFile");
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
if you want to delete folder after recording use this in function of stopping:
boolean deleted = file.delete();
I hope it can be helpful.

Record an audio for 30 seconds using android built in application in android

I am working on new app development in android. I need to launch an audio recording app from my current appication and i have launched it by calling intent using
MediaStore.Audio.Media.RECORD_SOUND_ACTION
But now i need to get control to the media which is being recorded. I need to record only for 30 seconds. the recording should be stopped when it reaches 30 seconds.
Any suggestions??
i think CountDownTimer will be help you ::
CountDownTimer countDowntimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
try {
Toast.makeText(context, "Stop recording Automatically ", Toast.LENGTH_LONG).show();
recorder.stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}};countDowntimer.start();
Use the MediaRecorder like so :
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public AudioRecorder(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;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("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();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
Use a Timer/TimerTask to call stop() after 30 seconds.
Source: tutorial

Categories

Resources