4

我正在尝试开发记录音频和播放的应用程序。我能够记录和播放录制的文件。现在我想在录制音频或声音时显示录制时间。搜索谷歌和许多东西都无法得到任何想法。谁能告诉我如何进行下一步。

这是我用计时器录制音频的代码

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);
}
}

这里我的 logcat 错误

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)

谢谢...

4

1 回答 1

9

为此,您应该尝试 Chronometer,这样您就可以实现您的目标,祝您好运

Chronometer 的示例代码在这里:

在 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>

在java文件中

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());

   }});


   }
}
于 2012-05-16T10:09:18.203 回答