Music does not play after checking checkbox again

User786 :
public class Options extends AppCompatActivity {
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music01);
    }    

    public void playSong(View view) {
        CheckBox musicCheck = findViewById(R.id.musicCheck);
        if (musicCheck.isChecked()) {
            mediaPlayer.start();
        }
        else {
            mediaPlayer.stop();
        }
    }
}

When I check the checkbox, the music starts playing and when I uncheck the checkbox, the music stops playing. However, when I check the checkbox again the music does not play.

isaaaaame :

Add this in onCreate

musicCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (musicCheck.isChecked()) {
            mediaPlayer.start();
        } else {
            mediaPlayer.stop();
        }
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152215&siteId=1