所以我根据此wesbite
的代码提供了这款非常具有Spartan风格的应用
它的目的是,当用户点击记录按钮时,它会提示他或她输入文件的名称,然后记录声音。用户可以录制他或她想要的声音,直到他们返回主菜单。
然而,当我第一次启动程序时,当我按下录制按钮时什么都不会发生。当我再次点击它时,它将在名称提示符期间开始录制。
这是什么问题?在代码中,我怀疑问题是我初始化保存文件名为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 :(得分:1)
将代码置于setPositiveButton
内,而不是在显示AlertDialog后进行检查,因为Android中的Dialogs是异步的。
答案 1 :(得分:0)
可能是你的记录按钮有android:focusableInTouchMode =“true”?