2014-04-20 50 views
0

所以我有一個基於代碼這個非常的Spartan-尋找應用程序從這個wesbiteAndroid應用程序將無法錄製聲音的第一個按鈕,點擊

enter image description here

它的目的,是當用戶點擊記錄按鈕,它會提示他或她輸入文件名稱,然後錄製聲音。用戶可以錄製儘可能多的聲音,直到他們回到主菜單爲止。

但是,當我第一次啓動該程序時,當我點擊錄製按鈕時,什麼都不會發生。當我再次點擊它時,它將在名稱提示過程中開始錄製。

這裏有什麼問題?在代碼中,我懷疑問題是我初始化了保存文件名的字符串變量爲null,但是我在用戶點擊取消的時候這樣做了,所以程序不會將聲音記錄到名爲null的聲音文件中。用戶第一次嘗試啓動程序時給了它一個名字,不應該通過空檢查嗎?

此外,程序是否應該在根據我的代碼啓動刻錄機之前等待名稱設置?

的代碼:

import java.io.IOException; 


import android.media.MediaRecorder; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.ActionBar; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class RecordASound extends Activity { 


    Button start; 
    Button stop; 
    Button mainMenu; 
    private MediaRecorder myRecorder; 
    private String outputFile = null; 
    private String value = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_record_asound); 



     start = (Button)findViewById(R.id.Record); 
     start.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startRecord(v); 
      } 
      }); 

      stop = (Button)findViewById(R.id.StopRecord); 
      stop.setEnabled(false); 
      stop.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       stopRecord(v); 
      } 
      }); 

      mainMenu = (Button)findViewById(R.id.MainMenu); 
      mainMenu.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
        mainMenu(v);  
      } 
      }); 


     // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // For the main activity, make sure the app icon in the action bar 
      // does not behave as a button 
      ActionBar actionBar = getActionBar(); 
      actionBar.setHomeButtonEnabled(false); 
     }//end if 

    }//end onCreate 

    public void startRecord(View view){ 

     AlertDialog.Builder alert = new AlertDialog.Builder(this); 

     alert.setTitle("Name of Sound File"); 
     alert.setMessage("Please give your sound file a name."); 

     // Set an EditText view to get user input 
     final EditText input = new EditText(this); 
     alert.setView(input); 

     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
     value = input.getText().toString(); 

      } 
     }); 

     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       value = null; 

      } 
     }); 

     alert.show(); 

      //if the user hit cancel on the prompt, back out of the recorder 
     if (value == null){ 
      return; 
     }//end if 
     else{ 
      outputFile = Environment.getExternalStorageDirectory(). 
        getAbsolutePath() + "/" + value + ".3gpp"; 

      myRecorder = new MediaRecorder(); 
      myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
      myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
      myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
      myRecorder.setOutputFile(outputFile); 


      try { 
        myRecorder.prepare(); 
        myRecorder.start(); 
     } catch (IllegalStateException e) { 
        // start:it is called before prepare() 
        // prepare: it is called after start() or before setOutputFormat() 
          e.printStackTrace(); 
        } catch (IOException e) { 
      // prepare() fails 
        e.printStackTrace(); 
       } 

       start.setEnabled(false); 
       stop.setEnabled(true); 

       Toast.makeText(getApplicationContext(), "Start recording...", 
           Toast.LENGTH_SHORT).show(); 

     }//end else 



    }//end startRecord 

    public void stopRecord(View view){ 
     try { // TODO Auto-generated method stub 
      myRecorder.stop(); 
      myRecorder.release(); 

      myRecorder = null; 

      stop.setEnabled(false); 
      start.setEnabled(true); 

      Toast.makeText(getApplicationContext(), "Stop recording...", 
        Toast.LENGTH_SHORT).show(); 
     } catch (IllegalStateException e) { 

      e.printStackTrace(); 
     } catch (RuntimeException e){ 
      //no valid audio/video data was collected 
      e.printStackTrace(); 

     }//end catch 

    }//end stopRecord 

    public void mainMenu(View view){ 

     startActivity(new Intent(RecordASound.this, MainActivity.class)); 

    }//end mainMenu 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.record_asound, menu); 
     return true; 
    }//end onCreateOptionsMenu 

}//end class 
+0

嘗試在'startRecord'函數的開頭將'value'的值初始化爲'null'。 – marven

+0

這沒有奏效。它現在使它根本不記錄。 我最終做的事情是在開始時將它初始化爲一個空白字符串,並且只有在您點擊取消時它纔會爲空。 但現在它根本不會記錄。 – user1768884

+0

嘗試將所有內容放在'setPositiveButton'函數中,而不是在顯示警告之後進行檢查。 – marven

回答

1

把裏面的代碼setPositiveButton代替具有AlertDialog是Android示爲對話框之後檢查是異步的。

0

可能是你的記錄按鈕有android:focusableInTouchMode =「true」?

相關問題