Home > Software engineering >  How to start music after 10 second is remaining on timer in Android Studio using JAVA
How to start music after 10 second is remaining on timer in Android Studio using JAVA

Time:09-24

So I made one minute countdown timer. Basically I want to start music(10seconds long) after 10 second is remaining on timer with SoundPool. This is my code so far:

public class BeginAfter extends AppCompatActivity {

    TextView textView;
    private SoundPool soundPool;
    private int tensec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin_after);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        soundPool = new SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build();
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        tensec = soundPool.load(this, R.raw.timer_run, 1);

        textView = findViewById(R.id.timer);

        long duration = TimeUnit.MINUTES.toMillis(1);

        new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long l) {
                String sDuration = String.format(Locale.ENGLISH, "d", TimeUnit.MILLISECONDS.toSeconds(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(l)));
                textView.setText(sDuration);
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(BeginAfter.this, Begindup.class);{
                    startActivity(intent);
                    finish();
                }
            }
        }.start();

CodePudding user response:

Try this. This seems right to me. Please note that you are defining soundPool twice. I'm not an expert, but that seems unnecessary to me. Judging by what I found online, you only have to write the second definition in case your API level is lower than android lollipop. This is where I saw the definitions.

public class BeginAfter extends AppCompatActivity {

    TextView textView;
    private SoundPool soundPool;
    private int tensec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin_after);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        soundPool = new SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build();
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); // this seems very unnecessary to me, as you are defining soundPool the line above.
        tensec = soundPool.load(this, R.raw.timer_run, 1);

        textView = findViewById(R.id.timer);

        long duration = TimeUnit.MINUTES.toMillis(1);

        new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long l) {
                String sDuration = String.format(Locale.ENGLISH, "d", TimeUnit.MILLISECONDS.toSeconds(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(l)));
                textView.setText(sDuration);
                if (l / 1000 == 50){
                    soundPool.play(tensec, 1, 1, 0, 0, 1);
                }
            }

            @Override
            public void onFinish() {
                soundPool.release();
                Intent intent = new Intent(BeginAfter.this, Begindup.class);{
                    startActivity(intent);
                    finish();
                }
            }
        }.start();

Here's a solution using MediaPlayer

public class BeginAfter extends AppCompatActivity {

    TextView textView;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin_after);
        mediaPlayer = MediaPlayer.create(this, R.raw.timer_run);

        textView = findViewById(R.id.timer);

        long duration = TimeUnit.MINUTES.toMillis(1);

        new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long l) {
                String sDuration = String.format(Locale.ENGLISH, "d", TimeUnit.MILLISECONDS.toSeconds(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(l)));
                textView.setText(sDuration);
                if (l / 1000 == 50){
                    mediaPlayer.start();
                }
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(BeginAfter.this, Begindup.class);{
                    startActivity(intent);
                    finish();
                }
            }
        }.start();
  • Related