0

I'm making app to measure windspeeds based on the noise of the mic input. The recording of the noise and the calculatoin to Decibells is working fine. I want to display the value of newAmplitude in a TextView called amptv. It only displays 0 because newAmplitude is 0 on onCreate. How can i change the text of the TextView into the value of newAmplitude after i hit the stoprecord button??

Since the onclicklistener is in its own class, i cant manage to do it just with amptv.setText(newAmplitude);

This is the last thing i tried, also tried some other things but they didn't worked out either.

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);
    }
}

hope someone can help me, i'm a beginner with java and im really stuck atm.

This is my second try, still get an error when i hit the stoprecord button.

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);
    }
}
Stiloboy
  • 33
  • 2
  • 9
  • Have you looked at these threads? http://stackoverflow.com/questions/1021167/refresh-a-view-android http://stackoverflow.com/questions/4503458/update-view-at-runtime-in-android – Alex Mar 02 '12 at 15:30

2 Answers2

0

Use your logic like this.

// (R.id.empty)     Replace with your TextView id.
TextView amptv = (TextView) findViewById(R.id.amptv);
amptv.setText(newAmplitude);

In your seperate area of class where you want to set the value, make an instance of the object. then set the value by setText(). This will exactly works fine for you.

Hamza Waqas
  • 629
  • 4
  • 12
0

Make the OnClickListener an inner class of Vumeter (you should use upper case identifiers for classes), then you can access the field amptv of Vumeter like this:

Vumeter.this.amptv.setText( Double.toString(Vumeter.this.newAmplitude) );

this would be the OnClickListener, Vumeter.this is the outer instance of Vumeter.

Stefan
  • 4,645
  • 1
  • 19
  • 35
  • What do you exactly mean by making it an inner class, does the onclicklistener have to be outside of the class RecordButton. i inserted the method you gave into my current code, i got an error when i pressed the recordbutton again to stop recording. my new code is on top of this page. – Stiloboy Mar 03 '12 at 13:33
  • It is an (anonymous, meaning it has no name) inner class in your code now. That is because it is defined inside the RecordButton class. However, amptv and newAmplitude are defined in vumeter, not in RecordButton. Thus you must set the listener in the vumeter class. After that check if you still need the RecordButton class. – Stefan Mar 03 '12 at 14:23