4

I am trying to play an mp3 file (with an onClickListener) and stop after 2 seconds. I tried the code below but it is not working. Could anyone please help?

final MediaPlayer mpsound = MediaPlayer.create(this, R.raw.media_player_sound);

ImageView sound = (ImageView) findViewById(R.id.button);

sound.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mpsound.start();{
                    sleep(2000);
                    mpsound.stop();
                }
            }
        });
ElTakaco
  • 43
  • 1
  • 4

2 Answers2

1

Why are you calling stop() on mpfrog if you are playing audio with mpsound? You need to call the stop() function on the mpsound MediaPlayer. Also, you might want to add the @Override annotation to your onClick() method.

for the override...

sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mpsound.start();{
                    sleep(2000);
                    mpsound.stop();
                }
            }
        });

for a timer.....

Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                mpsound.stop();
            }
        };

//Task for timer to execute when time expires
    class SleepTask extends TimerTask {
        @Override
        public void run(){
            handler.sendEmptyMessage(0);
        }
    }

//then in some other function...
Timer timer = new Timer("timer",true);
timer.schedule(new SleepTask(),2000);
Joel
  • 4,732
  • 9
  • 39
  • 54
  • I'm sorry I messed up the names but have corrected them now. I am new to Android so I am not sure where to put the @Override annotation. would it be right above the line "sound.setOnClickListener(new View.OnClickListener()"? – ElTakaco Jul 19 '12 at 17:48
  • no, it would go directly above the function you are overriding, in this case that is onClick()... see edit^ – Joel Jul 19 '12 at 17:55
  • am i right that sleep(2000) in UI thread will freeze the whole device ? – dilix Jul 19 '12 at 18:00
  • It will call sleep on the main thread of your application, which in this case, is the thread that the UI is on... so yeah, the UI will become useless for two seconds if you do it that way – Joel Jul 19 '12 at 18:01
  • So i think freeze UI while waiting is not a good (even very bad) idea =) – dilix Jul 19 '12 at 18:04
  • could you please show me how to implement the ContDownTimer with my function? I am new to Android and Java so I need to see where the countdowntimer would be in the code. thank you in advance – ElTakaco Jul 19 '12 at 18:44
  • Look at my edit above.. use a timer and schedule a timer task to execute after a certain amount of time. You will also need that handler to execute a task signaled by another thread. – Joel Jul 19 '12 at 18:48
0

You have to wait 2 seconds in different thread, for this case use your own created thread and wait there and stop player, or you can use CountDownTimer - very simple solution.

You can find same question aggregated here and here

In eclipse you can use autocomplete (ctrl + space by default) to fill automatically all inmplemented methods and @Overrides will already be there.

Community
  • 1
  • 1
dilix
  • 3,761
  • 3
  • 31
  • 55
  • I am new to Android and Java could you please explain where exactly to put the CountDownTimer please? I tried the following but it did not work: – ElTakaco Jul 19 '12 at 18:41
  • sound.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mpsound.start(); new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { } public void onFinish() { mpsound.stop(); } }.start(); } }); – ElTakaco Jul 19 '12 at 18:41