Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4047760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:43:52+00:00 2026-05-20T13:43:52+00:00

I am working on a media player app. I am trying to sync it

  • 0

I am working on a media player app. I am trying to sync it to a seekbar so that when media is played the seek bar progresses automatically. Here is my source code:

public class MusicDroid extends ListActivity implements
        SeekBar.OnSeekBarChangeListener, Runnable {

    private int cur_time = 0;
    private SeekBar seekbar;
    int song_dur = 0;
    GroveService gs;
    public Chronometer timer;
    int timer_time = 0;

    @Override
    public void onCreate(Bundle icicle) {
        try {
            super.onCreate(icicle);
            setContentView(R.layout.songlist);
            gs = new GroveService();
            gs.updateSongList();
        } catch (NullPointerException e) {
            Log.v(getString(R.string.app_name), e.getMessage());
        }
        seekbar = (SeekBar) findViewById(R.id.SeekBar01);
        timer = (Chronometer) findViewById(R.id.Chronometer01);
        seekbar.setOnSeekBarChangeListener(this);
        // this.bindService(new Intent(this, GroveService.class), conn,
        // Context.BIND_AUTO_CREATE);
        ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
                R.layout.song_item, gs.songs);
        setListAdapter(songList);
        timer.setOnChronometerTickListener(new OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer arg0) {
                // TODO Auto-generated method stub
                seekbar.setProgress(cur_time);
            }
        });
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        timer.stop();
        timer.start();
        gs.playSong(position, 0);
    }

    /*
     * private ServiceConnection conn= new ServiceConnection(){
     * 
     * @Override public void onServiceConnected(ComponentName arg0, IBinder
     * arg1) { Log.v("Status:","Service Connected to player"); // TODO
     * Auto-generated method stub tprog= new Thread(new Runnable(){
     * 
     * }); tprog.setPriority(Thread.MIN_PRIORITY); tprog.start(); }
     * 
     * @Override public void onServiceDisconnected(ComponentName arg0) { // TODO
     * Auto-generated method stub
     * 
     * } };
     */
    // code for menu based media player controls
    /*
     * public boolean onOptionsItemSelected(MenuItem item) { switch
     * (item.getItemId()) { case R.id.prev: gs.prevSong(); break; case
     * R.id.play: gs.playSong(gs.songindex,cur_time); break; case R.id.pause:
     * gs.mp.getDuration(); gs.mp.pause(); break; case R.id.nxt: gs.nextSong();
     * break; } return true; }
     */

    public void myClickHandler(View view) {
        switch (view.getId()) {
        case R.id.ImageButton01:
            gs.prevSong();
            timer.stop();
            seekbar.setMax(gs.getSongDuration(gs.songindex));
            Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
            timer.start();
            break;
        case R.id.ImageButton02:
            Thread t=new Thread();
            t.start();
            gs.playSong(gs.songindex, cur_time);
            timer.start();
            seekbar.setMax(gs.getSongDuration(gs.songindex));
            Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
            break;
        case R.id.ImageButton03:
            cur_time = gs.getCurrentDuration(gs.songindex);

              if(gs.mp.isPlaying()){
              timer_time=Integer.parseInt((String)timer.getText());
              timer.stop(); 
              timer.setBase(timer_time);
              Log.v("Timer Time",String.valueOf(timer_time)); }
              gs.mp.pause(); 
            seekbar.setMax(gs.getSongDuration(gs.songindex));
            Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
            break;
        case R.id.ImageButton04:
            gs.nextSong();
            timer.stop();
            timer.start();
            seekbar.setMax(gs.getSongDuration(gs.songindex));
            Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
            break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        gs.mp.seekTo(arg1);
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        int currentPosition = 0;
        int total = gs.mp.getDuration();
        seekbar.setMax(total);
        while (gs.mp != null && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = gs.mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            seekbar.setProgress(currentPosition);
        }
    }
}

Service class that performs music playback. code is here:

class Mp3Filter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3"));
    }
}

public class GroveService extends Service {
    static final String MEDIA_PATH = new String("/sdcard/");
    List<String> songs = new ArrayList<String>();
    MediaPlayer mp = new MediaPlayer();
    int songindex = 0;
    int duration=0;
    //MusicDroid md;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    public void onCreate()
    {
        super.onCreate();
        updateSongList();
        //md=new MusicDroid();
    }

    public void updateSongList() {
        File home = new File(MEDIA_PATH);
        if (home.listFiles(new Mp3Filter()).length > 0) {
            for (File file : home.listFiles(new Mp3Filter())) {
                songs.add(file.getName());
            }                                   
        }
    }
    public void playSong(final int position, final int cur_pos) {
        try {                     
            mp.reset();         
            mp.setDataSource(MEDIA_PATH + songs.get(position)); 
            mp.prepare();   
            //song_dur=mp.getDuration();
            mp.seekTo(cur_pos); 
            mp.start(); 
            duration=position;
            mp.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer arg0) {
                    nextSong();
                }
            });         
        } catch (IOException e) {
            Log.v(getString(R.string.app_name), e.getMessage());
        }
    }    

    public void nextSong() {
        if (++songindex >= songs.size())
            songindex = 0;
        else
            playSong(songindex,0);
    }

    public void prevSong() {
        songindex--;
        if (songindex > 0)
            playSong(songindex,0);
        else {
            songindex = 0;
            playSong(songindex,0);
        }
    }
    public int getSongDuration(int index)
    {   
        duration=mp.getDuration();
        Log.v("Song Duration", String.valueOf(duration));
        return duration;
    }
    public int getCurrentDuration(int index){
        int current_position;
        current_position=mp.getCurrentPosition();
        Log.v("Current Dur:",String.valueOf(current_position));
        return current_position;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:43:53+00:00Added an answer on May 20, 2026 at 1:43 pm

    in MediaPlayer you can set onPreparedListener in which you can synchronize your seek as as follow

    mMediaPlayer.setOnPreparedListener(new OnPreparedListener() 
              {
                @Override
                public void onPrepared(final MediaPlayer mp) 
                {
                    seekBar.setMax(mp.getDuration());
                       new Thread(new Runnable() {
    
                               @Override
                               public void run() {
                                       while(mp!=null && mp.getCurrentPosition()<mp.getDuration())
                                       {
                                           seekBar.setProgress(mp.getCurrentPosition());
                                           Message msg=new Message();
                                           int millis = mp.getCurrentPosition();
    
                                           msg.obj=millis/1000;
                                           mHandler.sendMessage(msg);
                                            try {
                                                Thread.sleep(100);
                                            } 
                                            catch (InterruptedException e) {
                                               e.printStackTrace();
                                            }
                                       }
                               }
                       }).start();
    
                }
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I working on a distributed mediaplayer that uses the windows media player component. Now
I'm working on an ASP.NET app that allows users to upload video files. After
I'm working on an app that will both record an audio file, and then
I am working on a media player for an example application... and things are
I'm working on a project fuzzing a media player. I wrote the file generator
I'm working on a streaming media application that pushes a lot of data to
Hai friends I am trying to display thumbnail videos in my media player and
I am currently working on media player. When I am clicking the next button
hi i have create one simple media player.... now its working fine. i retrieve
Working with a SqlCommand in C# I've created a query that contains a IN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.