gpt4 book ai didi

android - 第一次单击时,Android App不会录制声音

转载 作者:行者123 更新时间:2023-12-03 00:49:07 26 4
gpt4 key购买 nike

因此,我有一个基于此wesbite中的代码的外观非常酷的Spartan应用程序

这样做的目的是,当用户单击“录制”按钮时,它将提示他或她输入文件名,然后录制声音。用户可以录制任意数量的声音,直到返回主菜单。

但是,当我第一次启动该程序时,单击“录制”按钮将不会发生任何事情。当我第二次点击它时,它将在名称提示下开始记录。

这是什么问题?在代码中,我怀疑问题是我将保存文件名的字符串变量初始化为null,但是在用户单击“取消”的情况下这样做了,因此程序不会将声音记录到名为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

最佳答案

将AlertDialog显示为Android中的对话框是异步的后,将代码放在setPositiveButton中,而不要进行检查。

关于android - 第一次单击时,Android App不会录制声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com