I am trying to develop app, which records audio & playback.I am able to record & play recorded files. Now i want to display recording time while recording audio or sound. Searched google & many things not able to get it any idea. can anybody say me how to proceed next steps.
here is my code for audio recording with timer
public class AudioRecordActivity extends Activity implements OnClickListener {
MediaRecorder recorder = new MediaRecorder();
private String fileName;
private Button record;
private Button play;
private Button stop;
private TextView timeDisplay;
Chronometer myChronometer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
record = (Button) findViewById(R.id.recordButton);
record.setOnClickListener(this);
play = (Button) findViewById(R.id.playButton);
play.setOnClickListener(this);
stop = (Button) findViewById(R.id.stopButton);
stop.setOnClickListener(this);
myChronometer = (Chronometer) findViewById(R.id.timer);
record.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myChronometer.start();
}
});
stop.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myChronometer.stop();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.recordButton:
try {
stop.setEnabled(true);
record.setEnabled(false);
startRecording();
} catch (IOException e) {
System.out.println(e + "");
e.printStackTrace();
}
break;
case R.id.stopButton:
stop.setEnabled(false);
record.setEnabled(true);
stopRecording();
break;
case R.id.playButton:
Intent intent = new Intent();
// PLAY ANY AUDIO/VIDEO FILE
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/" + fileName);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
break;
default:
break;
}
}
void startRecording() throws IOException {
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
// fileName = "audio_" + timeStampFormat.format(new Date()) + ".mp4";
fileName = "audioTest" + ".mp4";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/" + fileName);
recorder.setAudioEncodingBitRate(320);
recorder.setAudioSamplingRate(16000);
recorder.setAudioChannels(2);
recorder.prepare();
recorder.start();
}
private void stopRecording() {
if (null != recorder) {
try {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
} catch (NullPointerException e) {
Toast.makeText(getApplicationContext(),
"Recording not started", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (KeyEvent.KEYCODE_BACK == keyCode) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
Here My logcat error
05-16 16:11:11.603: E/AndroidRuntime(21256): FATAL EXCEPTION: main
05-16 16:11:11.603: E/AndroidRuntime(21256): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.com/xxx.com.AudioRecordActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Chronometer
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.os.Looper.loop(Looper.java:137)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-16 16:11:11.603: E/AndroidRuntime(21256): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 16:11:11.603: E/AndroidRuntime(21256): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 16:11:11.603: E/AndroidRuntime(21256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-16 16:11:11.603: E/AndroidRuntime(21256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-16 16:11:11.603: E/AndroidRuntime(21256): at dalvik.system.NativeStart.main(Native Method)
05-16 16:11:11.603: E/AndroidRuntime(21256): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Chronometer
05-16 16:11:11.603: E/AndroidRuntime(21256): at xxx.com.AudioRecordActivity.onCreate(AudioRecordActivity.java:45)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.Activity.performCreate(Activity.java:4465)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-16 16:11:11.603: E/AndroidRuntime(21256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
Thanks...
for this you should try Chronometer, by this you can fulfill your goal good luck
sample code of Chronometer is here :
in main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Chronometer
android:id="@+id/chronometer"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/buttonstart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
/>
<Button
android:id="@+id/buttonstop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
/>
<Button
android:id="@+id/buttonreset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Reset"
/>
</LinearLayout>
in java file
package com.exercise.AndroidChronometer;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class AndroidChronometer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
Button buttonStart = (Button)findViewById(R.id.buttonstart);
Button buttonStop = (Button)findViewById(R.id.buttonstop);
Button buttonReset = (Button)findViewById(R.id.buttonreset);
buttonStart.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myChronometer.start();
}});
buttonStop.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myChronometer.stop();
}});
buttonReset.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myChronometer.setBase(SystemClock.elapsedRealtime());
}});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With