I want to use Media Recorder to record and save an audio file to the sd card. The debugger has shown that the line recorder.start() raises an IllegalStateException. The code is taken from the android dev website and the only change is to the file name and path.
When I reach the error in the debugging menu, I am shown the View class which says:
Source not found.
The source attachment does not contain the source for the file View.class.
You can change the source attachment by clicking Change Attached Source below:
canRecord is a boolean set initially to true, which dictates which method is called in an onClick function. That functionality is working.
#SuppressLint("SimpleDateFormat")
private void record(){
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM/dd/yyyy");
String audio_path = Environment.getExternalStorageDirectory() + "/resources/resources/WI1/";
String fileName = username +"-"+ timeStampFormat.format(new Date())+".mp4";
audio_button.setText("Recording");
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(audio_path+fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Toast.makeText(this, "Can't record audio at this time", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
canRecord = false;
recorder.start();
} // record
This is the stop recording function, although it has never reached this point.
private void stopRecording(){
audio_button.setText("Attach Audio");
recorder.stop();
recorder.release();
}
Finally, I have added the following permissions to the manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How can I solve this? Thanks!
May be there is problem in filename you are giving to the recorder.Try this:
String audio_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/resources/resources/WI1/";
String fileName = audiopath+"-"+ timeStampFormat.format(new Date())+".mp4";
May it helps..
I am using libgdx(multiplatform open source game engine) and i had a similar problem when i was trying to use androids code instead of the libgdx code for recording.Turned out i needed to dispose of my libgdx recorder code or just delete it all.So perhaps you have another recorder interfering somewhere
Related
I want to create a microphone app on Android that will receive sound through the microphone and play through the speakerphone but I don't know exactly what classes and services I should use.
The core of your answer is to:
A) record and store, as stated here.
MediaRecorder recorder = new MediaRecorder();
String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
String path = Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"; // your custom path
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // notice that this is the audio format, and you might want to change it to [other available audio formats][2]
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
// To start recording
recorder.start();
// To stop recording
recorder.stop();
recorder.release();
} else {
// Handle the situation
}
[Other available audio formats | 2]
B) Get recordings, as stated partially here. You should then show them.
try {
String path = Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"; // your custom path
File directory = new File(path);
File[] files = directory.listFiles();
} catch {
// Handle errors (or maybe no files in the directory)
}
C) Play recordings, as stated partially here.
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"+"/yourfilename.formatextension"; // your custom pathare to use the file format and directory you used when saving
mp.prepare();
mp.start();
Hope this answer helps!
The android application I am working on; it has an option of audio-recording; I want it to save a new audio recorded file in internal storage of a device so that no neither user nor other applications will be able to access to those recorded audio files, unless they open my application.
My main struggle is to be able to save that audio file in internal storage: I took my time to review in my android programming books and read some questions and answers here : How to save an audio file in internal storage android and https://developer.android.com/guide/topics/data/data-storage#filesInternal and https://www.tutorialspoint.com/android/android_internal_storage.htm but unfortnately things I am getting from there are not settling my problem at all.
The codes that I am using to record are here below :
private MediaRecorder rec;
private String file_folder="/scholar/";
String
file_path=Environment.getExternalStorageDirectory().getPath();
File file= new File(file_path,file_folder);
Long date=new Date().getTime();
Date current_time = new Date(Long.valueOf(date));
rec=new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
rec.setAudioChannels(1);
rec.setAudioSamplingRate(8000);
rec.setAudioEncodingBitRate(44100);
rec.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (!file.exists()){
file.mkdirs();
}
/*the file here this one:File file= new File(file_path,file_folder);
it means the value of the outputfile is the one which will be provided by
rec.setOutputFile() method, so it will be saved as this name:
file.getAbsolutePath()+"/"+"_"+current_time+".amr"
*/
rec.setOutputFile(file.getAbsolutePath()+"/"+"_"+current_time+".amr");
try {
rec.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Recording_Service.this,"Sorry! file creation failed!",Toast.LENGTH_SHORT).show();
return;
}
rec.start();
rec.stop();
rec.reset();
Unfortunately what I am doing there, is saving my file on external storage, it means after recording, the file is visible to everyone in a device file explorer, they can even delete the file.
So! Please, I need your help guys, if any help, I will appreciate it. Thanks!
I had found the answer of this question, I used getFilesDir();
private MediaRecorder rec;
String file_path=getApplicationContext().getFilesDir().getPath();
File file= new File(file_path);
Long date=new Date().getTime();
Date current_time = new Date(Long.valueOf(date));
rec=new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
rec.setAudioChannels(1);
rec.setAudioSamplingRate(8000);
rec.setAudioEncodingBitRate(44100);
rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (!file.exists()){
file.mkdirs();
}
String file_name=file+"/"+current_time+".3gp";
rec.setOutputFile(file_name);
try {
rec.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Recording_Service.this,"Sorry! file creation failed!"+e.getMessage(),Toast.LENGTH_SHORT).show();
return;
}
rec.start();
rec.stop();
rec.reset();
Thanks for everyone who tried to help me for the answer. Let's continue to enjoy coding to make our world more sweet pleasant and enjoyable.
I am recording video with audio using MediaRecorder, however when you take pictures with Camera you can save the image into a Bitmap and display it before saving, I want to do the same with the video - like Snapchat does, is there a way? I don't want to save the video, display it, and then have the option to delete or keep it.
I am using this code in order to record videos:
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SelfieLightCamera/";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
String myFile = path + "Video_" + System.currentTimeMillis() + ".mp4";
mediaRecorder = new MediaRecorder();
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOutputFile(myFile);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
e.printStackTrace();
} catch (IOException e) {
releaseMediaRecorder();
e.printStackTrace();
}
mediaRecorder.start();
The cool thing about Android apps is that you can extract the APK from your phone and decompile the package to see the actual Java code. Although it's usually obfuscated (as is the case with Snapchat), you can still get quite a good view into the inner workings of the application.
When looking into how Snapchat does it, I found out that they also use MediaRecorder just like you but save the file into internal storage. Internal storage means that the file is only accessible by the app that holds it.
Here's a few lines of code that gives you a general idea of Snapchat does behind the scenes:
mediaRecorder.setCamera(ay$b.b());
mediaRecorder.setAudioSource(5);
if (z) {
mediaRecorder.setVideoSource(2);
} else {
mediaRecorder.setVideoSource(1);
}
mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setVideoSize(i, i2);
mediaRecorder.setMaxFileSize(bm.a());
mediaRecorder.setVideoEncodingBitRate(bm.a(camcorderProfile));
// Removed some code hereā¦
mediaRecorder.setOrientationHint(this.f);
mediaRecorder.setMaxDuration(HttpService.DEFAULT_READ_TIMEOUT);
mediaRecorder.setOutputFile(this.a.toString());
if (this.c != null) {
mediaRecorder.setPreviewDisplay(this.c);
What you should do is record the video normally and save it to the internal storage. If you want to "keep" it, you just move it to the external storage where it's publicly available. Discarding it means that you just delete it from the internal storage.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
MediaRecorder: setCamera() - error camera is not aviable
I am making an application that allows a user to record audio and save it somewhere in the SD card. I am using a MediaRecorder to do record the audio.
I am reusing some of the code from the androiddevblog website as it was recommended by another user on stackoverflow to check those tutorials.
My problem is whenever I click the button to record audio I get an error saying "Your Application has been forced to stop". I have posted my code for the recording feature below.
EDIT: I solved my original problem. Now when I add recorder.stop() to my code I get an illegalStateException. I have updated the code below as well(The only changes are in the startRecorder method). Any ideas ?
I am aware that nothing will get recorded based on my code. I want to first make sure the file gets created and saved.
public class MyRecorderActivity extends Activity{
private Button audio;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionandanswer);
....
....
audio = (Button) findViewById(R.id.audio_recordactivity);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startRecording();
}
});
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath() + "/");
}
private 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());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.stop();
recorder.reset();
recorder.release();
}
}
I solved my problem. I forgot to add the permissions!
I am making an application that allows a user to record audio and save it somewhere in the SD card. I am using a MediaRecorder to do record the audio.
I am reusing some of the code from the androiddevblog website as it was recommended by another user on stackoverflow to check those tutorials.
My problem is whenever I click the button to record audio I get an error saying "Your Application has been forced to stop". I used the debugger to find out that it is because I have an illegalStateException on recorder.stop()
I am aware that nothing will get recorded based on my code. I want to first make sure the file gets created and saved.
I posted this question before, and solved my original problem but ran into this one after. So it may seem as a duplicate.
public class MyRecorderActivity extends Activity{
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp"
private Button audio;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionandanswer);
....
....
audio = (Button) findViewById(R.id.audio_recordactivity);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startRecording();
}
});
}
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() + AUDIO_RECORDER_FILE_EXT_3GP);
}
private 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());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.stop();
recorder.reset();
recorder.release();
}
}
Make sure to include following permission.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
May be another recording is active(May be a default recorder or recorderr of another downloaded application)stop that recording and do the operation again, hope it works