0

私はこの方法を持っています、

    if(null != recorder){
      recorder.stop();
      recorder.reset();
      recorder.release();
      recorder = null;
    }

アプリケーションを強制終了するのはなぜですか? ...

前もって感謝します!

これは私の VoiceRecording2.java です。ボタン、開始ボタン、停止ボタンがあり、フォーマットを選択します。

 package com.example.voicexml;

 import java.io.File;
 import java.io.IOException;

 import android.app.Activity;
 import android.app.AlertDialog;
  import android.content.DialogInterface;
 import android.media.MediaRecorder;
  import android.os.Bundle;
 import android.os.Environment;
 import android.view.View;
import android.widget.Button;

 public class VoiceRecording2 extends Activity {
    private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
    private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
    private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

    private MediaRecorder recorder = null;
    private int currentFormat = 0;
    private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
    private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP }; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voice);

    setButtonHandlers();
    enableButtons(false);
    setFormatButtonCaption();
}

    private void setButtonHandlers() {
            ((Button)findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button)findViewById(R.id.btnStop)).setOnClickListener(btnClick);
    ((Button)findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
    }

    private void enableButton(int id,boolean isEnable){
            ((Button)findViewById(id)).setEnabled(isEnable);
    }

    private void enableButtons(boolean isRecording) {
            enableButton(R.id.btnStart,!isRecording);
            enableButton(R.id.btnFormat,!isRecording);
            enableButton(R.id.btnStop,isRecording);
    }

    private void setFormatButtonCaption(){
            ((Button)findViewById(R.id.btnFormat)).setText(getString(R.string.audio_format) + " (" + file_exts[currentFormat] + ")");
    }

    private String getFilename(){
            String filepath = Environment.getExternalStorageDirectory().getPath();
            File file = new File(filepath,AUDIO_RECORDER_FOLDER);

            if(!file.exists()){
                    file.mkdirs();
            }

            return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
    }

    private void startRecording(){
            recorder = new MediaRecorder();

            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(output_formats[currentFormat]);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(getFilename());

            recorder.setOnErrorListener(errorListener);
            recorder.setOnInfoListener(infoListener);

            try {
                    recorder.prepare();
                    recorder.start();
            } catch (IllegalStateException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
    }

    private void stopRecording(){

    }

    private void displayFormatDialog(){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            String formats[] = {"MPEG 4", "3GPP"};

            builder.setTitle(getString(R.string.choose_format_title))
                       .setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                    currentFormat = which;
                                    setFormatButtonCaption();

                                    dialog.dismiss();
                            }
                       })
                       .show();
    }

    private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
            public void onError(MediaRecorder mr, int what, int extra) {
                    AppLog.logString("Error: " + what + ", " + extra);
            }
    };

    private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
            public void onInfo(MediaRecorder mr, int what, int extra) {
                    AppLog.logString("Warning: " + what + ", " + extra);
            }
    };

private View.OnClickListener btnClick = new View.OnClickListener() {
            public void onClick(View v) {
                    switch(v.getId()){
                            case R.id.btnStart:{
                                    AppLog.logString("Start Recording");

                                    enableButtons(true);
                                    startRecording();

                                    break;
                            }
                            case R.id.btnStop:{
                                if(null != recorder){
                                    //recorder.stop();
                                    //recorder.reset();
                                    //recorder.release();
                                    System.out.println("Churva");
                                    //recorder = null;
                            }
                                AppLog.logString("Start Recording");

                                    enableButtons(false);

                                    //stopRecording();

                                    break;
                            }
                            case R.id.btnFormat:{
                                    displayFormatDialog();

                                    break;
                            }
                    }
            }
    }; 

}

これは、Android デバイスで音声を録音する単純なプログラムです。

4

2 に答える 2

1

まず、MediaRecorder クラス (android.media.MediaRecorder) の新しいインスタンスを作成します。

MediaRecorder mr = new MediaRecorder();

次に、音源または録音デバイスを設定します。通常は MIC に設定します。

mr.setAudioSource(MediaRecorder.AudioSource.MIC);

次に、出力形式を指定します。これは、録音されたファイルが保存されるオーディオ形式です。

mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

また、AudioEncoder タイプを指定します

mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

最後に、録音データを保存するファイル名を指定します。パス名は、オーディオ ファイルへのフル パスです。

mr.setOutputFile(PATH_NAME);

これで、セットアップ全体が完了しました。呼び出してインスタンスを準備し、関数をprepare()呼び出して記録を開始/停止するだけです。start()stop()

mr.prepare();mr.start();.......mr.stop();

記録が終了したら、呼び出して、その特定のインスタンスに関連付けられているリソースを解放できます。

mr.release();

を呼び出して、MediaRecorder インスタンスを初期状態にリセットすることもできます。

mr.reset();

必要に応じて、 を使用mr.setMaxDuration()して、記録の最大時間を設定したり、記録にmr.setMaxFileSize()使用する最大ファイル サイズを設定したりすることもできます。

<uses-permission android:name="android.permission.RECORD_AUDIO">マニフェストに追加

于 2013-01-11T11:42:17.360 に答える
1

Check the Java text of stop() method of recorder which says:

{public void stop () Since: API Level 1 Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed.

Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

Throws IllegalStateException if it is called before start()}

So may be exception is arising due to no valid audio/video data has been received when stop() method is called.

于 2013-01-10T07:10:34.897 に答える