2

MediaRecorder で録音したファイルの名前を変更しようとしています。これが私がこれにアプローチした方法です。

まず、startRecording() メソッドと stopRecording メソッドを作成しました。

public void startRecording (){
     recorder = new MediaRecorder();
     recorder.reset();      
     recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
     {
         externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
         externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
         recorder.setOutputFile(externalOutputPath);
     }
     else
     {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
     }
     recorder.setOnErrorListener(errorListener);
     recorder.setOnInfoListener(infoListener);

     try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}

これは、記録を開始および停止する方法です。

recBtn.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
                startRecording();
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
                stopRecording();
                nameAlert();
            }
            return true;
        }

    });
} //END OF ONCREATE

そこで次にやりたかったのは、録音ボタンを押すのをやめるたびに出力ファイルの名前を設定できるようにすることです。記録を停止するたびに呼び出される新しいメソッドを作成して、それを実行しようとしました。

public void nameAlert() {
    final EditText etFileName = (EditText) findViewById(com.whizzappseasyvoicenotepad.R.id.etFileName);
    Button okBtn = (Button) findViewById(com.whizzappseasyvoicenotepad.R.id.okBtn);

    final Dialog dialog = new Dialog (getApplicationContext(), android.R.style.Theme_Translucent);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(com.whizzappseasyvoicenotepad.R.layout.alert_name);

    okBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            fileName = etFileName.getText().toString();
            recorder.setOutputFile(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/" + fileName + ".mp3");
            dialog.dismiss();
        }
    });
}

次に、stopRecording(); を呼び出した直後にこのメソッドを追加しました。

記録を停止するたびに(画像ボタンのタッチを停止して)、アプリがクラッシュします。これは logcat ファイルです。

08-01 22:39:26.790: E/InputEventReceiver(8258): Exception dispatching input event.
08-01 22:39:26.790: E/MessageQueue-JNI(8258): Exception in MessageQueue callback: handleReceiveCallback
08-01 22:39:26.806: E/MessageQueue-JNI(8258): java.lang.NullPointerException
08-01 22:39:26.806: E/MessageQueue-JNI(8258):   at com.whizzappseasyvoicenotepad.MainActivity.nameAlert(MainActivity.java:117)
08-01 22:39:26.806: E/MessageQueue-JNI(8258):   at com.whizzappseasyvoicenotepad.MainActivity$3.onTouch(MainActivity.java:59)
08-01 22:39:26.822: E/AndroidRuntime(8258): FATAL EXCEPTION: main
08-01 22:39:26.822: E/AndroidRuntime(8258): java.lang.NullPointerException
08-01 22:39:26.822: E/AndroidRuntime(8258):     at com.whizzappseasyvoicenotepad.MainActivity.nameAlert(MainActivity.java:117)
08-01 22:39:26.822: E/AndroidRuntime(8258):     at com.whizzappseasyvoicenotepad.MainActivity$3.onTouch(MainActivity.java:59)

エラーは 117 行目と 59 行目にあります。

59 行目:

nameAlert();

117行目:

okBtn.setOnClickListener(new OnClickListener() {

私はまだ初心者のプログラマーなので、間違った方法でアプローチした可能性があります。プログラマーとしての私の考え方はまだ完璧ではありません。どうすればこれを行うことができるか教えてください。ありがとうございました!

レイアウト ファイル:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/dim"
android:padding="30dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="129dp"
    android:text="Enter the name of the recorded file:"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/etFileName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/okBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etFileName"
    android:layout_centerHorizontal="true"
    android:text="Ok" />

</RelativeLayout>
4

1 に答える 1

0

アプリに特定のデザインが本当に必要でない限り、単純なダイアログ ビルダーを使用してみませんか? edittext とボタンをプログラムでダイアログに追加することもできます。このようなもの:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your message");
final EditText input = new EditText(this);
builder.setView(input);

次に、ポジティブボタンとネガティブボタンを設定するだけでOKです。

于 2013-08-05T12:12:18.383 に答える