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 8199929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:16:19+00:00 2026-06-07T06:16:19+00:00

I am making an android application where i got a seekbar for some music.

  • 0

I am making an android application where i got a seekbar for some music. I got the seekbar to show the music playing status, where i currently am in the music playback. But how can i setup a listener so that i can say that the mediaplayer would play a specific place on a track? I know the code for setting the play time in mediaplayer, but how can i handle the click events from seekbar? like onClick() or anything similar?

FIXED:
My code:

package com.mycompany.appname;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SlidingDrawer;

public class MusicActivity extends ListActivity implements OnClickListener, OnSeekBarChangeListener {
//Called when the activity is first created
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
  static final String[] COUNTRIES = new String[] {
      "Movies"
      };
  MediaPlayer mp1;
  String musicUri;
  Button PausePlay;
  int positionInt;
  SlidingDrawer slidingDrawer2;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.musicscreen);

    //Import views
    PausePlay = (Button)findViewById(R.id.PausePlay);
    slidingDrawer2 = (SlidingDrawer)findViewById(R.id.slidingDrawer2);

    //Setup onClickListener for the buttons
    PausePlay.setOnClickListener(this);

    //Setup mediaPlayer
    mp1 = new MediaPlayer();

    //Setup ListView
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

    //Setup seekBar
    final SeekBar progress = (SeekBar)findViewById(R.id.musicProgressBar);
    progress.setOnSeekBarChangeListener(this);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    File mFile = new File(Environment.getExternalStorageDirectory() + "/Music/");
    myList.addAll(Arrays.asList(mFile.list()));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        //When an item is clicked, play it on a MediaPlayer
          //Play song
          positionInt = position;
          if (mp1.isPlaying()) {
              //Reset mediaPlayer
              mp1.reset();
              try {
                  musicUri = Environment.getExternalStorageDirectory() + "/Music/" + myList.get(positionInt);
                mp1.setDataSource(musicUri);
                mp1.prepare();
                mp1.start();
                slidingDrawer2.animateOpen();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          } else {
          try {
              musicUri = Environment.getExternalStorageDirectory() + "/Music/" + myList.get(position);
            mp1.setDataSource(musicUri);
            mp1.prepare();
            mp1.start();
            slidingDrawer2.animateOpen();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

          //Setup seekBar
          final SeekBar progress = (SeekBar)findViewById(R.id.musicProgressBar);
          progress.setProgress(0);
          progress.setMax(mp1.getDuration());
          new CountDownTimer(mp1.getDuration(), 250) {
            public void onTick(long millisUntilFinished) {
                if (progress.getProgress() == mp1.getDuration()) {
                    mp1.reset();
                    progress.setProgress(0);
                } else {
                  progress.setProgress(mp1.getCurrentPosition());
                }
            }
            public void onFinish() {}
          }.start();
        }
  }
    });
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (fromUser == true) {
        mp1.seekTo(seekBar.getProgress());
    } else {
        //Do nothing
    }
}

@Override
public boolean onKeyDown(int KeyCode, KeyEvent event) {
    if ((KeyCode == KeyEvent.KEYCODE_BACK)) {
        if (mp1.isPlaying()) {
            mp1.reset();
            System.exit(0);
        } else {
            finish();
        }
        return true;
    }
    return super.onKeyDown(KeyCode, event);
}

public void onClick(View src) {
    switch(src.getId()) {
    case R.id.PausePlay:
        if (mp1.isPlaying()) {
            mp1.pause();
            PausePlay.setText("Play");
        } else {
            mp1.start();
            PausePlay.setText("Pause");
        }
        break;
    }
}

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

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

My xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Musikk"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >




    <ListView
        android:id="@+android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    </ListView>


    <SlidingDrawer
        android:id="@+id/slidingDrawer2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/content"
        android:handle="@+id/handle" >


        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Spiller nå" />


        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >


            <SeekBar
                android:id="@+id/musicProgressBar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >













                <Button
                    android:id="@+id/PausePlay"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Pause" />

            </LinearLayout>

        </LinearLayout>
    </SlidingDrawer>

</FrameLayout>

</LinearLayout>
  • 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-06-07T06:16:20+00:00Added an answer on June 7, 2026 at 6:16 am

    You’ll want to implement SeekBar.OnSeekBarChangeListener, then implement the onProgressChanged method.

    This is called when the progress is changed (eg a user click somewhere on the seekbar), and will provide you with the new position.

    Example:

    public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
        // Do something
    }
    

    Don’t forget to register the OnSeekBarChanged listener with seekBar.setOnseekBarChangeListener

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an android application to show the position of a user on
I'm currently making a SMS Application in Android, the following is a code snippet
I'm working on an Android application which can make internet phone calls. I got
I'm currently making a basic maths application in Android. Program will print on std
I'm making an Android application that has to read some values from a JSON
I'm making an Android application and it has to load some data though Internet
I'm making an android application in which I want to show Punjabi text in
Hey guys, i am making an android application where i want to show a
I'm making a small Android application to show current total CPU usage like tab
i am currently making one android application, i moded title bar, so all content

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.