0

I have recently started android programming and it is totally new to me.I want to update sound id and drawable id after some interval of time, but nothing is updated@.

For the time I am using:

updatetime =  mediaPlayer.getDuration();

Here is Activity code:

public class Alpha_Loop extends AppCompatActivity {

ImageView abc,img1;
int i = 0;
int playsound;
Runnable Rn;
int s = 0;
long updatetime;
MediaPlayer mediaPlayer;

//Array of Image1
final int[] ImageArray = {R.drawable.a, R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e,
        R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k,
        R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q,
        R.drawable.r, R.drawable.s, R.drawable.t, R.drawable.u, R.drawable.v, R.drawable.w,
        R.drawable.x, R.drawable.y, R.drawable.z};

//Array of Image1
final int[] img1Array = {R.drawable.apple, R.drawable.ball, R.drawable.cat, R.drawable.dog, R.drawable.egg,
        R.drawable.fish, R.drawable.goat, R.drawable.hat, R.drawable.insect, R.drawable.jar,
        R.drawable.king, R.drawable.leaf, R.drawable.monkey, R.drawable.nose, R.drawable.ocean,
        R.drawable.parrot, R.drawable.queen, R.drawable.rat, R.drawable.sun, R.drawable.tap,
        R.drawable.umbrella, R.drawable.violin, R.drawable.well, R.drawable.box, R.drawable.yatch,
        R.drawable.zebra};
//Array of wav files
final int[] Soundid = {R.raw.a, R.raw.a, R.raw.b, R.raw.c, R.raw.d, R.raw.e,
        R.raw.f, R.raw.g, R.raw.h, R.raw.i, R.raw.j, R.raw.k,
        R.raw.l, R.raw.m, R.raw.n, R.raw.o, R.raw.p, R.raw.q,
        R.raw.r, R.raw.s, R.raw.t, R.raw.u, R.raw.v, R.raw.w,
        R.raw.x, R.raw.y, R.raw.z};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alpha__loop);
    abc = findViewById(R.id.abc);
    img1 = findViewById(R.id.img1);
    playsound = Soundid[0];
    abc.setImageResource(R.drawable.a);
    img1.setImageResource(R.drawable.apple);

   mediaPlayer = MediaPlayer.create(Alpha_Loop.this, playsound);
    mediaPlayer.start();
    mediaPlayer.setLooping(false);

    final Handler handler = new Handler();
    Rn = new Runnable() {
        @Override
        public void run() {
            abc.setImageResource(ImageArray[i]);
            i++;
            if (i >= ImageArray.length) {
                i = 0;
            }
            img1.setImageResource(img1Array[i]);
            i++;
            if (i >= img1Array.length) {
                i = 0;
            }
            playsound = Soundid[s];
            s++;
            updatetime =  mediaPlayer.getDuration();
            if (s >= Soundid.length) {
                s = 0;
            }
            handler.postDelayed(Rn, updatetime);
        }

    };
}

}

Any help will be helpful for me.

Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
Tanveer Ahmed
  • 426
  • 1
  • 4
  • 15
  • if you add a breakpoint to the updatetime in your runnable, what value are you getting? – Nikos Hidalgo Dec 18 '18 at 15:34
  • I am getting values which are initially set seems like Runable isn't executed – Tanveer Ahmed Dec 18 '18 at 15:36
  • on my end I think your code raises a few questions, but the most important one for me is what is the value in millis that the getDuration assign to your updatetime. That's the time you tell your handler to wait before it does anything – Nikos Hidalgo Dec 18 '18 at 15:41
  • yes it plays a raw and I am getting duration of that raw by `mediaplayer.getDuration()` and assigning that long as updatetime in Runnable – Tanveer Ahmed Dec 18 '18 at 15:44
  • I know that, I mean what is the actual value you're getting. I'm sensing you missed my breakpoint question because you haven't used them before. Have a look here first https://developer.android.com/studio/debug/#startdebug It will help you in many ways if you want to carry on with android development (and this specifically https://developer.android.com/studio/debug/#breakPoints ) – Nikos Hidalgo Dec 18 '18 at 15:47
  • I am getting these messages in debugger `E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present E/MediaPlayer: Should have subtitle controller already set E/MediaPlayer: Should have subtitle controller already set E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present E/MediaPlayer: Should have subtitle controller already set E/MediaPlayer: Should have subtitle controller already set` – Tanveer Ahmed Dec 18 '18 at 15:55
  • this is informational and most likely irrelevant to your problem with the values as it's mentioned here: https://stackoverflow.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-android – Nikos Hidalgo Dec 18 '18 at 16:01

2 Answers2

0

the time that goes in the handler's postDelayed method, shows how long the handler needs to delay before calling the runnable (the delay -in milliseconds- until the runnable will be executed). So at the part of your code where you need the runnable to run, call handler.post(Rn); to start your runnable.

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
0

I'm pretty sure it will work if you do it like this:

final Handler handler = new Handler();
Rn = new Runnable() {
    @Override
    public void run() {
        abc.setImageResource(ImageArray[i]);
        i++;
        if (i >= ImageArray.length) {
            i = 0;
        }
        img1.setImageResource(img1Array[i]);
        i++;
        if (i >= img1Array.length) {
            i = 0;
        }
        playsound = Soundid[s];
        s++;
        updatetime =  mediaPlayer.getDuration();
        if (s >= Soundid.length) {
            s = 0;
        }
    }
};
handler.postDelayed(Rn, updatetime);

Your handler was doing postDelayed u=in runnable and runnable is therefor never executed.

EDIT: Don't use such a short name with first uppercase letter since it's against Java naming convention.

TheKarlo95
  • 1,144
  • 9
  • 17