Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SeekBar to control MediaPlayer progress

Tags:

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

like image 413
Ilya Blokh Avatar asked Feb 28 '12 12:02

Ilya Blokh


People also ask

What is a Seek bar in video player?

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.

How to play song in Android programmatically?

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.

How does Android Media player work?

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.


1 Answers

The problem is seekBar.setOnSeekBarChangeListener(this); So put it in Your AudioControl() Method..... You dont set oncheckchangedlistner to your seekbar....

like image 126
Samir Mangroliya Avatar answered Oct 22 '22 01:10

Samir Mangroliya