0

In my application, there is 1 main activity and there are many fragments that are inflated by the activity.

In one of the fragments, there is a task that runs for 10 seconds. Along with this task, background music needs to be played for this duration.

How can I achieve this?

Any help will be appreciated.

ss1234
  • 13
  • 5
  • What you are doing is performing an animation that last for 10 seconds. – rgv Mar 29 '18 at 19:28
  • thank you @rgv Yes we have a task running with timer for 10 seconds and progress bar with animation. we want to add the music when the timer starts and play the music for 10 seconds... – ss1234 Mar 29 '18 at 19:30
  • I'll try to add the code in, if you get errors in it you will have to research and see what is going on, but it should be enough to get you on your way – rgv Mar 29 '18 at 19:33
  • Thankyou for the code... but we want to add the animation using the progressbar too with the timer... can we add music to the existing objectanimatorprogressbar? – ss1234 Mar 29 '18 at 19:44
  • Don;t use the object animator to play music, that is not what it is for, run a separate thread or handler/runnable to stop the music playing after 10 seconds – rgv Mar 29 '18 at 19:45

2 Answers2

0

You can play music using MediaPlayer as follows

MediaPlayer mp = MediaPlayer.create(this, idSong);
mp.start();

You can stop the music after 10 seconds by using a Timer as follows

new java.util.Timer().schedule(
    new java.util.TimerTask() {
        @Override
        public void run() {
            mp.pause();
        }
}, 10000);
Scott Smith
  • 3,900
  • 2
  • 31
  • 63
Daan Seuntjens
  • 880
  • 1
  • 18
  • 37
0
private void startEverything() {
    startMyAnimation();
    playMusic()
}

private void startMyAnimation() {
    ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgress, "progress", 0, 10000);
    progressAnimator.setDuration(10000);
    progressAnimator.setInterpolator(new LinearInterpolator());
    progressAnimator.start();
}

private void playMusic() {
    final MediaPlayer soundPlayer= MediaPlayer.create(getContext(), R.raw.your_bgm);
    soundPlayer.start();


    Handler musicStopHandler = new Handler();
    Runnable musicStopRunable = new Runnable() {
        @Override
        public void run() {
            soundPlayer.stop();
        }
    };

    musicStopHandler.postDelayed(musicStopRunable, 10000);
}

What happens here is, you start your animation by calling startMyAnimation(), then you call playMusic() to play your music.

In playMusic(), you start playing the file by calling soundPlayer.start();, then you start run a Handler that will execute the code inside 'musicStopRunnable', and the code in musicStopRunnable, stops the music playing.

reference : https://stackoverflow.com/a/18459304/4127441

https://stackoverflow.com/a/1921759/4127441

rgv
  • 1,186
  • 1
  • 17
  • 39
  • There are different Handler classes in Android, the Handler to use in this case is : android.os.Handler.java – rgv Mar 29 '18 at 19:48