views:

44

answers:

3

I know you can 'seekto()' with Mediaplayer... (To start at a certain point)

But does anyone know if there is a way to make a track (the audio playing)... Stop at a certain point? -or would an if statement on a timer loop have to be used?

Please help...

Thanks alot,

James

A: 

Doesn't seem possible (correct me if I'm wrong) to do this with media player without resorting to seekto() in a timer loop. However you could try using an AudioTrack in conjunction with setting a notification marker:

AudioTrack.setNotificationMarkerPosition

Sets the position of the notification marker.

and overriding the playback position update listener AudioTrack.OnPlaybackPositionUpdateListener

Interface definition for a callback to be invoked when the playback head position of an AudioTrack has reached a notification marker or has increased by a certain period.

stealthcopter
I'll try a timer loop, =]I'm not much into effiency, but i'm guessing a timer loop is inefficient? :P :)
James Rattray
I'd say it was messier more than inefficient, probably wont slow app down noticeably.
stealthcopter
A: 

Sadly, AudioTrack's position callbacks appear to be fairly seriously broken. http://code.google.com/p/android/issues/detail?id=2563

Mark
A: 

You have to make threat that will trigger getCurrentPosition().

When it will reach stop point, you have to stop MediaPlayer.

public void run() {
    while (mp != null && mPosition < mTotal) {
        try {
            Thread.sleep(500); // you can modify sleep time for better accuracy
            if (mp.isPlaying()) {
                mPosition = mp.getCurrentPosition();
                if (mPosition == mYourStopPoint) { //remember to set mYourStopPoint
                    mp.stop();
                    break;
                }
            }
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }

    }
}

Start this Thread in onPreapared callback.

public void onPrepared(MediaPlayer genericPlayer) {
    mTotal = mp.getDuration();
    new Thread(this).start();
}
darbat