我正在制作应用程序,根据麦克风输入的噪音测量风速。将噪音和计算器记录到Decibells工作正常。我想在名为 amptv 的TextView中显示 newAmplitude 的值。它只显示0,因为 newAmplitude 在onCreate上为0。 在点击“停止记录”按钮后,如何将TextView的文本更改为 newAmplitude 的值?
由于onclicklistener属于自己的类,我无法使用amptv.setText(newAmplitude)进行操作;
这是我尝试过的最后一件事,也尝试了其他一些事情,但他们也没有成功。
public class vumeter extends Activity {
private static final String LOG_TAG = "AudioRecordTest";
protected static final String TAG = null;
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
currentAmplitude = mRecorder.getMaxAmplitude();
Log.i(TAG, "handleMessage : MaxAmplitude : " + currentAmplitude);
newAmplitude = 20 * Math.log(currentAmplitude);
Log.i(TAG, "nieuwe amplitude : " + newAmplitude);
}
};
private Timer timer = new Timer();
private TimerTask timerTask = null;
private int currentAmplitude;
private double newAmplitude = 5.0;
public TextView amptv;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private synchronized void startRecording() {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
timer.schedule(timerTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
}
}, 250, 250);
}
private synchronized void stopRecording() {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop measuring");
} else {
setText("Start measuring");
amptv.setText(newAmplitde);
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start measuring");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public vumeter() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
TextView amptv = new TextView(this);
ll.addView(amptv, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
amptv.setText(Double.toString(newAmplitude));
setContentView(ll);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.help:
startActivity(new Intent(this, InfoMeter.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
希望有人可以帮助我,我是java的初学者,我真的很困难。
这是我的第二次尝试,当我按下stoprecord按钮时仍然会出错。
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop measuring");
} else {
setText("Start measuring");
vumeter.this.amptv.setText(Double.toString(vumeter.this.newAmplitude));
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start measuring");
setOnClickListener(clicker);
}
}
答案 0 :(得分:0)
像这样使用你的逻辑。
// (R.id.empty) Replace with your TextView id.
TextView amptv = (TextView) findViewById(R.id.amptv);
amptv.setText(newAmplitude);
在要设置值的类的单独区域中,创建对象的实例。然后通过setText()设置值。这对你来说非常合适。
答案 1 :(得分:0)
使OnClickListener
成为Vumeter
的内部类(您应该使用类的大写标识符),然后您可以访问amptv
的字段Vumeter
,如下所示:
Vumeter.this.amptv.setText( Double.toString(Vumeter.this.newAmplitude) );
this
将是OnClickListener
,Vumeter.this
是Vumeter
的外部实例。