Button Audio stopper not stopping the Button

23 views Asked by At

I have a problem with my code its supposed to stop the button when clicked on so like i click on Button1 it plays click on Button1 again it stops etc. etc. and also for other buttons the same works and when you click on Button2 for example Button1 Stops

I have 74 buttons and this is in oncreate for all of them

Button1Text = (Button) findViewById(R.id.Button1Text);
Button1Text2 = (Button) findViewById(R.id.Button1Text2);

this is the code for the button

 public void Button1Text(View view) {
       normalButton();
       Button1Text.setBackgroundResource(R.color.lighterbrown);

       releaseAll();
       media1 = MediaPlayer.create(QuranJ30MA.this, R.raw.hannaba);
       media1.start();
    }

    public void Button1Text2(View view) {
       normalButton();
        Button1Text2.setBackgroundResource(R.color.lightergreen);

        releaseAll();
        media2 = MediaPlayer.create(QuranJ30MA.this, R.raw.mannaba);
        media2.start();
    }

this is the "releaseMP" function that supposed to do what i want it to do but doesnt work in this app while it works in other apps

    private void releaseMP(MediaPlayer mediaPlayer) {
        try {
            if (mediaPlayer != null) {
                if (mediaPlayer.isPlaying())
                    mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

this is the "releaseAll();" function

   private void releaseAll() {
        releaseMP(media1);
        releaseMP(media2);

i think thats all also the "normalButton();" just resets colors

i tried to copy it exactly the same from other codes of my other apps(note they work in those apps but not here) i dont understand where i went wrong this is a problem i have had for days

1

There are 1 answers

0
Md Eusuf Uddin On

Set startRecodingButton with OnClickListener:

startStopRecodingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startStopRecoding();
        }
    });

Check MediaRecorder is null or not.

private MediaRecorder mRecorder;
private void startStopRecoding() {
    if(mRecorder==null){
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(mFileName);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e("TAG", "prepare() failed");
        }
        // start method will start
        // the audio recording.
        mRecorder.start();
        startStopRecodingButton.setText("Stop Recoding");
    }else {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
        startStopRecodingButton.setText("Start Recoding");
    }
}