0

Hello I would like to develop an app in Android that displays a countdown and plays an individual sound when it expires. The app has to be in the foreground the whole time.

Problems are still the sound and the app in the foreground.

Unfortunately I have no idea, as I have only recently started developing with Android.

Thanks for the help!

I have used this template and integrated a method loadMusic, how can I now pause this instead of stop and play music individually?

private void loadMusic() {
    if (timerStatus == TimerStatus.PAUSED)
        new Runnable() {
            @Override
            public void run() {
                playSound(R.raw.sound1);
            }
            public void playSound(int sound1) {
                MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.raw.sound2));
                mp.start();
            }
        }.run();
}

My Sound class:

package de.codeyourapp.countdowntimer;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class Sound extends AppCompatActivity {

ImageView imageButton1, imageButton2, imageButton3, imageButton4;

@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sound);

    imageButton1 = findViewById(R.id.iv_bowl1);
    imageButton2 = findViewById(R.id.iv_bowl2);
    imageButton3 = findViewById(R.id.iv_bowl3);
    imageButton4 = findViewById(R.id.iv_bowl5);

    MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.sound1);
    MediaPlayer mediaPlayer2 = MediaPlayer.create(this, R.raw.sound2);
    MediaPlayer mediaPlayer3 = MediaPlayer.create(this, R.raw.sound3);
    MediaPlayer mediaPlayer4 = MediaPlayer.create(this, R.raw.sound5);

    imageButton1.setOnClickListener(v -> mediaPlayer1.start());
    imageButton2.setOnClickListener(v -> mediaPlayer2.start());
    imageButton3.setOnClickListener(v -> mediaPlayer3.start());
    imageButton4.setOnClickListener(v -> mediaPlayer4.start());
    }
}
rico
  • 1
  • 3

1 Answers1

-1

You can use CountDownTimer of android. You can check this post for help.

Destroy the timer in onPause() to ensure that the timer doesn't work on background.

As to sound - you can add a sound file in your asset folder and take help from this thread to learn how to play the sound.

ganjaam
  • 1,030
  • 3
  • 17
  • 29