android 根据if条件启动声音

android start sound based on if condition

我正在尝试制作一个简单的应用程序,您可以在其中根据可以通过单击按钮增加的 int 值播放声音,

例如我有一个文本框,里面有整数值,我可以增加文本框内的整数值或使用两个按钮减少它,我想做的是在整数值变得更大时播放音乐超过 10,我尝试这样做但声音没有开始,

这是我的代码:

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

出现问题,即使您没有播放它,您也会调用暂停。结果异常

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

为防止此类异常,请确保在使用 mp.isPlaying() 进行任何操作之前检查您的媒体播放器是否正在播放音乐。

例如

// to play
boolean needToPlay = result > 10;

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

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

完整示例

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