سؤال

I wanted to implement an function that plays only the last 5 seconds of an Audiofile (wich is longer than 5 seconds). Does Mediaplayer somehow support that?

I know how to normally play an Audio file + I know how to get the duration of the file, but how do I set the Mediaplayer to play only the short part at the end? So that means playing from a specific timecode in the Audiofile.

هل كانت مفيدة؟

المحلول

One way to implement this would be to use MediaPlayer.getDuration() method to get the audio file duration and then use the MediaPlayer.seetTo() API to move to that position..

In your case you can try mediaPlayer.seekTo(mediaPlayer.getDuration()-(5*1000));...

نصائح أخرى

You could make a call to seekTo after preparing.

Take a look at this example :

this._mediaPlayer = new MediaPlayer();
this._mediaPlayer.reset();
this._mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
this._mediaPlayer.setDataSource( _data_source_ );
this._mediaPlayer.setOnSeekCompleteListener(this);
this._mediaPlayer.prepare();
this._mediaPlayer.start();
/* Seeking to 1 minute */
this._mediaPlayer.seekTo(60 * 1000);
/* Issue a start in the OnSeekCompleteListener */

Adapt this example to get the duration of a stream once it is prepared and seek to the duration - 5 seconds (if the media is at least 5 seconds long, in your case).

However, it seems that Android has a bug related to the use of seekTo when seeking to an unbuffered par of the media. Take a look at this and this for more informations. This bug should have been resolved in Android 2.2.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top