Android Media Recorder preview before saving - android

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.

Related

How to access files saved programatically in Environment.DIRECTORY_DCIM in Windows Operating system?

My question is different from this one since I do not use a SD Card.
I take a picture using Camera API and here is how I save the file :
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
This works fine. When I browse in my phone native camera app or even in the phones documents, I see the files (i.e. the pictures).
**Problem : ** When the phone is connected to my laptop (Windows 10) using USB (whatever protocol is used here i.e MTP, PTP, USB), when I browse to the DCIM folder, I dont see the files. I though it was hidden, I tried to unhide it, but still it does not show.
Can you help me find this file so that I can copy it to my laptop for testing purposes ?
You can use Android Studio's File Explorer pane to access files in your phone/emulator
From your comment it seems that you store your pictures on the SDCard so you might access them from :

How to save an audio recorded in internal storage

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.

How to make reverse video and editing in video file in android programmatically? [duplicate]

I have created functionality to record video in my app.
When I play a song, that song is recorded with video and a video file is created, similar to a dubshmash application.
Now the problem that I am facing is that other voices such as near by sounds also get recorded. The song file is recorded in the video record screen and I play the song when video recording activity launches.
How can I have my application record only song with video?
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
Is there any solution in audio source set as a speaker , because song sound going through a speaker? if is it another possible way please reply me.
You can record video without audio and merge audio later on using mp4 parser like this:
/*
* #param videoFile path to video file
* #param audioFile path to audiofile
*/
public String mux(String videoFile, String audioFile) {
Movie video = null;
try {
video = new MovieCreator().build(videoFile);
} catch (RuntimeException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Movie audio = null;
try {
audio = new MovieCreator().build(audioFile);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
int size = audio.getTracks().size();
Track audioTrack = audio.getTracks().get((size - 1));
video.addTrack(audioTrack);
Container out = new DefaultMp4Builder().build(video);
File myDirectory = new File(Environment.getExternalStorageDirectory(), "/Folder Name");
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
filePath = myDirectory + "/video" + System.currentTimeMillis() + ".mp4";
try {
RandomAccessFile ram = new RandomAccessFile(String.format(filePath), "rw");
FileChannel fc = ram.getChannel();
out.writeContainer(fc);
ram.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return filePath;
}
In build.gradle add following dependency
compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'
If you want to working with video then you have to use FFMPEG library
That can be you can work with Video.
That for i have already give answer to How to use ffmpeg in android studio? see this LINK. Go step by step and import in your project
You can use a MediaRecorder without calling setAudio* on it.
remove this line
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
see this link
There is currently no way to directly record android output without "background noise".
Note that this is a security concern to restrict access to other apps audio output, therefore it is very unlikely that it could be achieved directly.
See this answer

How to video record with specific sound programmatically in android?

I have created functionality to record video in my app.
When I play a song, that song is recorded with video and a video file is created, similar to a dubshmash application.
Now the problem that I am facing is that other voices such as near by sounds also get recorded. The song file is recorded in the video record screen and I play the song when video recording activity launches.
How can I have my application record only song with video?
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
Is there any solution in audio source set as a speaker , because song sound going through a speaker? if is it another possible way please reply me.
You can record video without audio and merge audio later on using mp4 parser like this:
/*
* #param videoFile path to video file
* #param audioFile path to audiofile
*/
public String mux(String videoFile, String audioFile) {
Movie video = null;
try {
video = new MovieCreator().build(videoFile);
} catch (RuntimeException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Movie audio = null;
try {
audio = new MovieCreator().build(audioFile);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
int size = audio.getTracks().size();
Track audioTrack = audio.getTracks().get((size - 1));
video.addTrack(audioTrack);
Container out = new DefaultMp4Builder().build(video);
File myDirectory = new File(Environment.getExternalStorageDirectory(), "/Folder Name");
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
filePath = myDirectory + "/video" + System.currentTimeMillis() + ".mp4";
try {
RandomAccessFile ram = new RandomAccessFile(String.format(filePath), "rw");
FileChannel fc = ram.getChannel();
out.writeContainer(fc);
ram.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return filePath;
}
In build.gradle add following dependency
compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'
If you want to working with video then you have to use FFMPEG library
That can be you can work with Video.
That for i have already give answer to How to use ffmpeg in android studio? see this LINK. Go step by step and import in your project
You can use a MediaRecorder without calling setAudio* on it.
remove this line
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
see this link
There is currently no way to directly record android output without "background noise".
Note that this is a security concern to restrict access to other apps audio output, therefore it is very unlikely that it could be achieved directly.
See this answer

Surface view Camera not saving video

private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
// set the orientation here to enable portrait recording.
setCameraDisplayOrientation(this,0,myCamera);
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
String pathh=Environment.getExternalStorageDirectory().getPath();
Toast.makeText(getApplicationContext(), "Path is "+pathh,Toast.LENGTH_LONG).show();
mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
//mediaRecorder.setOutputFile("/storage/sdcard0/myvideo1.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50Mb
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
mediaRecorder.setOrientationHint(MainActivity.orientation);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
My task is to capture video using surface view and send that to server. I found 1st solution to capture video in surface and save it in memory using some stack overflow link. One is below.
Switch To Front Camera and Back Camera Android SurfaceView
At first the app starts and worked perfectly saved the video also. Then I deleted the video and then tried video was not saving in memory. I tried with renaming the file also not worked.
"lrwxrwxrwx" what is this code value mean in android. I find this in DDMS
The code is missing MediaScannerConnection.scanFile, which updates gallery. The code may be saving the videos, but the gallery will not show the videos. Restarting the phone will scan the gallery, and if videos were saved they will appear. Also, the files app will probably list the videos.
If the program is saving videos, add code at or near the end of the program, or override onPause:
MediaScannerConnection.scanFile(this, new String[]{videoPathName}, null, null);
videoPathName is a String you need to set to the path and name of the saved video.
If still not working, the following code should work, but you will still need to add the MediaScanner: http://sandyandroidtutorials.blogspot.com/2013/05/android-video-capture-tutorial.html

Categories

Resources