optimus
optimus

Reputation: 729

Stop MediaPlayer at certain point

Hi guys Im trying to stop mediaplayer in certain point but it doesnt work for me below is my code let me know what is wrong in this code.

int Str = 36000;
                int Stop = 51000;
                mediaPlayer.seekTo(Str);
                mediaPlayer.start();
                if(mediaPlayer.getCurrentPosition() == Stop)
                {
                    mediaPlayer.stop();
                }

Media player starts at the specified point and it continues to play, when I debug it, it comes to the if condition and it still continues to play no error(as far I know) is displayed, I played with while, if condition and debug line by line but cant figure out,Please help me Thanks for your time

Upvotes: 1

Views: 1449

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Try to bound the time:

while(mediaPlayer.getCurrentPosition() >= Str && mediaPlayer.getCurrentPosition() <= Stop)
{
}

However, use this wisely, you might freeze something and end up in ANR.

You mind find useful using Thread or Handler delaying for 1000ms, but as @dhaag23 mentioned, its unlikely to get exact Stop

Upvotes: 2

dhaag23
dhaag23

Reputation: 6126

Media player commands typically execute asynchronously so the chances of you're getting the current position to be exactly the "stop" position is unlikely. Maybe you want to change your test to:

if(mediaPlayer.getCurrentPosition() >= Stop)

Upvotes: 1

Related Questions