I have a SeekBar
, it displays correctly MediaPlayer
progress. However, I have troubles with seeking - if I seek scroll box somewhere it just returns on the position where audio file is playing.
public class EntityPageActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{ private SeekBar seekBar; private Button startMedia; private Button pauseMedia; private MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.entity_page); AudioControl(); } public void AudioControl(){ seekBar = (SeekBar) findViewById(R.id.seekBar); startMedia = (Button) findViewById(R.id.play); pauseMedia = (Button) findViewById(R.id.pause); startMedia.setOnClickListener(this); pauseMedia.setOnClickListener(this); } public void run() { int currentPosition= 0; int total = mp.getDuration(); while (mp!=null && currentPosition<total) { try { Thread.sleep(1000); currentPosition= mp.getCurrentPosition(); } catch (InterruptedException e) { return; } catch (Exception e) { return; } seekBar.setProgress(currentPosition); } } public void onClick(View v) { if (v.equals(startMedia)) { if (mp != null && mp.isPlaying()) return; if(seekBar.getProgress() > 0) { mp.start(); return; } mp = MediaPlayer.create(EntityPageActivity.this, R.raw.gl1); mp.start(); seekBar.setProgress(0); seekBar.setMax(mp.getDuration()); new Thread(this).start(); } if (v.equals(pauseMedia) && mp!=null) { mp.pause(); } } public void onStartTrackingTouch(SeekBar seekBar) { } public void onStopTrackingTouch(SeekBar seekBar) { } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(fromUser) mp.seekTo(progress); } }
Thus, onProgressChanged
isn't enough to allow SeekBar
to control MediaPlayer
?
What should I add/correct to allow seeking?
Thanks
UPDATE
I found out that onProgressChanged
is never invoked by some reason. However I can't figure out why
Well according to android.developers.com, A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.
Prepare media file: To play a media file, you need to first prepare it i.e. you need to load the file for playback. Methods used for doing this are prepare(), prepareAsync(), and setDataSource(). Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method.
You can play audio or video from media files stored in your application's resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs. Note: You can play back the audio data only to the standard output device.
The problem is seekBar.setOnSeekBarChangeListener(this);
So put it in Your AudioControl()
Method..... You dont set oncheckchangedlistner
to your seekbar....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With