Home > Enterprise >  android start sound based on if condition
android start sound based on if condition

Time:01-25

I am trying to make a simple app where you can play sound based on int value you can increase by clicking a button,

for example I have a textbox with integer value inside it and I can increase the value of the integer inside the textbox or decrease it using two buttons, what I want to do is to play music if the integer value become more than 10, I tried to do this but the sound is not starting,

here is my code:

 public class MainActivity extends AppCompatActivity {

String speed;
float nCurrentSpeed;
int num1;
int num2;
int result;
TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final MediaPlayer mp = MediaPlayer.create(this, R.raw.gg);

    tv = (TextView) findViewById(R.id.textView);

    Button btn2 = (Button) findViewById(R.id.button2);
    Button btn3 = (Button) findViewById(R.id.button3);

    btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            //  mp.start();
            result = result   1;
            tv.setText(""   result);
            if (result > 10) {
                mp.start();
            } else {
                mp.pause();
            }

        }
    });


    btn3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // mp.pause();
            result = result - 1;

            tv.setText(""   result);
            if (result > 10) {
                mp.start();
            } else {
                mp.pause();
            }
        }
    });
}
}

CodePudding user response:

There was problem, that you're calling pause even when you're not playing it. Resulting exception

MediaPlayerNative: pause called in state 0, mPlayer(0xb400007b4ec9caa0)
MediaPlayerNative: error (-38, 0)
MediaPlayer: Error (-38,0)

To prevent such exception, please make sure you check whether your media player is playing music before taking any action with mp.isPlaying().

For Example

// to play
boolean needToPlay = result > 10;

if (needToPlay && !mp.isPlaying()) {
    mp.start();
}
// to pause
boolean needToPause = result <= 10;

if (mp.isPlaying() && needToPause) {
    mp.pause();
}

Complete Example

public class MainActivity extends AppCompatActivity {

    String speed;
    float nCurrentSpeed;
    int num1;
    int num2;
    int result;
    TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.gg);

        tv = (TextView) findViewById(R.id.textView);

        Button btn2 = (Button) findViewById(R.id.button2);
        Button btn3 = (Button) findViewById(R.id.button3);

        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                result = result   1;
                tv.setText(""   result);
                
                //check if need to play or not
                boolean needToPlay = result > 10;
                
                // if need to play, play when it's already not playing
                if (needToPlay && !mp.isPlaying()) {
                    mp.start();
                }

            }
        });


        btn3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                result = result - 1;

                tv.setText(""   result);
                
                //check if need to play or not
                boolean needToPause = result <= 10;
                
                // if need to pause then pause only if it's playing
                if (needToPause && mp.isPlaying()) {
                    mp.pause();
                }
            }
        });
    }
}
  •  Tags:  
  • Related