【问题标题】:Android : Audio Recorder With TImerAndroid : 带定时器的录音机
【发布时间】:2012-05-16 09:27:47
【问题描述】:

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

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

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)

谢谢...

【问题讨论】:

    标签: android timer audio-recording


    【解决方案1】:

    为此,您应该尝试 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());
    
       }});
    
    
       }
    }
    

    【讨论】:

    • java.lang.ClassCastException: android.widget.TextView 不能转换为 android.widget.Chronometer ???检查这一行
    • unable to find solution for this error java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Chronometer ....如果有任何解决方案请告诉我..跨度>
    • 我得到了abv错误的解决方案..但我无法同时开始时间和录音..现在我无法播放音频
    • 谢谢..现在我用计时器得到输出..但现在必须用记录时间来控制计时器。我会尽力做到这一点.. 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2022-01-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多