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