how to add video timer in android - android

i am new in android. i am developing an application where i record a video and see the time of its recording.i cant add the timer part.can anyone help me out..
my camera recording part i s as follows---
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(recording){
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
//Exit after saved
finish();
}else{
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(MainActivity.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}
});

Check following link. Start timer on recording press button and stop it on recording stop button.
Android Timer Example

Timer is not part of the native recorder, you have to implement it yourself. If you need it only for the recording process just add a timer view. If you want to see the timer in the final result ask me how, it is more complicate to implement.

Related

Android pause media player when user opens the camera and start recording video

I am developing music play app. When user push my app to background song is playing fine. Now if user opens the camera and starts recording video from camera, I need to pause the song playing from my app .How to do this?
I expect that the app responsible for video recording will request audio focus to notify other apps that they should cease playback. If this is the case,
you can implement AudioManager.OnAudioFocusChangeListener like this:
#Override
public void onAudioFocusChange(int focusChange)
{
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// pause playback
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(this);
doStopPlayback();
}
// else if... listen to other types of audio focus loss/ gain
}
where doStopPlayback() is your method for releasing the MediaPlayer etc.
See also this guide for media playback.
You can check it using method Camera.open(cameraId).
Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.
Throws RuntimeException
If opening the camera fails (For Example, if the camera is in use by another process or device policy manager has disabled the camera).
Update:
Example:
public boolean isCameraUsebyApp() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (camera != null) camera.release();
}
return false;
}
You can use this method to use as but keep this thing in mind that this method first acquire the camera.
If its acquire successfully then its mean that no other application is using this camera and don't forgot to release it again otherwise you will not able to acquire it again.
Its its throws RuntimeException it means that camera is in use by another process or device policy manager has disabled the camera.

Time Difference Check- Android video capture

I am working on a video recording camera APP. App crashes if camera is stopped right after starting it maybe because of video size very less. I want to activate stop button only if video size is greater than 1 sec. But problem is I cannot find Current Time and Start time correctly. Finding the difference of two time factors will help in implementing 2 sec Check. Need Help please.
private void onClickActions(View v)
{
float tt = start_time /10000000000000f;
float ct = ((System.currentTimeMillis() ) /10000000000000f);
Log.d("Before stoping S-Time ",tt+"");
Log.d("Before stoping C-Time ",ct+"");
if (recording && tt>=2.0f)
{
Log.d("After Stopping = ",tt+"");
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
recording = false;
rec.setVisibility(View.INVISIBLE);
start_time = 0;
}
//remove time code to initial revert
if(v.getId()== start.getId() && ((CameraPreview.recordHappy || CameraPreview.recordSad))) {
prepareMediaRecorder();
recording = true;
mediaRecorder.start();
consent = true;
happyRecorded=true;
stop.setClickable(true);
start.setClickable(false);
if (AndroidVideoCaptureExample.iV.getVisibility()==View.VISIBLE)
AndroidVideoCaptureExample.iV.setVisibility(View.INVISIBLE);
//AndroidVideoCaptureExample.capture.setText("RECORDING STARTED!");
rec.setVisibility(View.VISIBLE);
start_time = (int)(System.currentTimeMillis());
//Toast.makeText(myContext, "You are being recorded now!", Toast.LENGTH_LONG);
}
if(v.getId()== stop.getId() && consent==true && recording==false) {
if((!CameraPreview.recordHappy && CameraPreview.recordSad))
{
releaseMediaRecorder(); // release the MediaRecorder object
Intent intent = new Intent();
intent.setClass(AndroidVideoCaptureExample.this, consentActivity.class);
startActivity(intent);
finish();
}
else {
CameraPreview.recordHappy = false;
CameraPreview.recordSad = true;
stop.setClickable(false);
start.setClickable(true);
recording = false;
AndroidVideoCaptureExample.capture.setText("Record Neutral Moment");
rec.setVisibility(View.INVISIBLE);
}
}
}
I think you might be overengineering a simple thing. You don't really need to count record time unless you are showing it on the UI. If you want to disable the button, simply disable it just before starting the recording, then use Handler to re-enable after 2 seconds:
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
// enable stop button
}
},2000);
However, I would argue that's not a very good user experience. If you look at cameras like Google Camera, you can stop it immediately after starting, it just won't record anything. To achieve this, you need to catch the RuntimeException when calling mediaRecorder.stop(), then check and clean up the generated file. If it's empty then delete it and don't throw an error to the UI.

Google Android Building A Camera App Tutorial

I'm currently going through the 'Building A Camera App' tutorial -http://developer.android.com/guide/topics/media/camera.html#custom-camera
As someone relatively new to android, I find it a bit confusing / unclear at times.
I'm trying to understand where this code is supposed to go:
private boolean isRecording = false;
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
setCaptureButtonText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
setCaptureButtonText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
}
);
Can this be anywhere within the top level class, or is this supposed to be inside one of the provided methods or inner classes?
No matter where I put this code it's causing errors telling me to add or remove '}', but I'm sure I must just have it in the wrong place, since I'm sure google's code is fine.
Any help much appreciated!
This code belongs in the activity that loads the layout containing the "button_capture" Button. One you find that activity, out this inside of the onCreate() method.
No matter where I put this code it's causing errors telling me to add or remove '}'
This is simply a matter of getting your braces paired up properly.

How to record audio on Android without crash?

I'm developing on a Nitro HD with Gingerbread. I want to record audio and I experience an infinite hang while calling MediaRecorder.stop().
I know that my phone can record sound because I have an application that does it exactly.
I read the book "Android for programmers" from Deitel et al. and there is the example VoiceRecorder in chapter 16. Everything seems fine but the app hangs forever when it calls MediaRecorder.stop(). Also, the resource is not released and I have to reboot the phone to release it.
Here is the part of the code where the calls are done (see Deitel et al., "Android for Programmers", Prentice Hall, 2012, chap 16):
// starts/stops a recording
OnCheckedChangeListener recordButtonListener =
new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if (isChecked)
{
visualizer.clear(); // clear visualizer for next recording
saveButton.setEnabled(false); // disable saveButton
deleteButton.setEnabled(false); // disable deleteButton
viewSavedRecordingsButton.setEnabled(false); // disable
// create MediaRecorder and configure recording options
if (recorder == null)
recorder = new MediaRecorder(); // create MediaRecorder
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(
MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
try
{
// create temporary file to store recording
File tempFile = File.createTempFile(
"VoiceRecorder", ".3gp", getExternalFilesDir(null));
// store File as tag for saveButton and deleteButton
saveButton.setTag(tempFile);
deleteButton.setTag(tempFile);
// set the MediaRecorder's output file
recorder.setOutputFile(tempFile.getAbsolutePath());
recorder.prepare(); // prepare to record
recorder.start(); // start recording
recording = true; // we are currently recording
handler.post(updateVisualizer); // start updating view
} // end try
catch (IllegalStateException e)
{
Log.e(TAG, e.toString());
} // end catch
catch (IOException e)
{
Log.e(TAG, e.toString());
} // end catch
} // end if
else
{
recorder.stop(); // stop recording
recorder.reset(); // reset the MediaRecorder
recording = false; // we are no longer recording
saveButton.setEnabled(true); // enable saveButton
deleteButton.setEnabled(true); // enable deleteButton
recordButton.setEnabled(false); // disable recordButton
} // end else
} // end method onCheckedChanged
}; // end OnCheckedChangedListener
In a debug session, the "else" scope is entered but it hangs on its first (stop()) line.
I repeat, I know the phone and its OS are correct because another app works correctly. So, do you have any idea on how to solve this problem, a work around maybe?
Thanks!
EDIT When the recorder is started(), there is a handler that is executed at each 50ms to display a graph of the amplitude of the sound. The method recorder.getMaxAmplitude() always returns 0. Maybe this is the symptom of a badly initialized MediaRecorder?
The argument of setAudioEncodingBitRate() might be too low.
what is good setAudioEncodingBitRate on record voice
Hope that's help.
You could have a null recorder at that spot. You're not creating a new MediaRecorder() if you enter the else case of isChecked.
So, 2 things:
The API docs state that if you call stop() before start you'll throw a RuntimeException And if you fail to record anything you'll throw an IllegalStateException.
Check recorder before calling stop:
if (recorder != null) {
recorder.stop();
// some recorder stuff here
}

MediaRecorder.stop() hangs on Samsung Galaxy Camera

Calling stop() on my MediaRecorder hangs indefinitely on the Samsung Galaxy Camera. Placing this call in a separate thread does not help the problem either.
Logcat does not show any error messages. However, running this same app does not incur any problems on the Samsung Galaxy Nexus.
This is the code surrounding my call to stop:
View.OnClickListener captureListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop();
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
captureButton.setText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
captureButton.setText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
};
One thing I saw is that for some devices MediaRecorder.stop() hangs if there is no preview attached (i.e. you called Camera.stopPreview() before or maybe you never called startPreview()).

Categories

Resources