Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends ActionBarActivity {
- private MediaPlayer song;
- private SeekBar seekbar;
- private final Handler handler = new Handler();
- private final Runnable updatePositionRunnable = new Runnable() {
- public void run() {
- updatePosition();
- }
- };
- //private boolean isPlaying = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment())
- .commit();
- }
- seekbar = (SeekBar)findViewById(R.id.musicSeekBar);
- song = MediaPlayer.create( MainActivity.this, R.raw.xyz);
- seekbar.setMax(song.getDuration());
- final Button playButton = (Button)findViewById(R.id.playButton);
- playButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if(song.isPlaying()){
- playButton.setText("Play");
- song.pause();
- //isPlaying = false;
- handler.removeCallbacks(updatePositionRunnable);
- }
- else{
- playButton.setText("Pause");
- song.start();
- //isPlaying = true;
- updatePosition();
- }
- }
- });
- song.setOnCompletionListener(new OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- playButton.setText("Play");
- //seekbar.setProgress(0);
- //mp.stop();
- }
- });
- seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- song.seekTo(progress);
- }
- });
- private void updatePosition(){
- handler.removeCallbacks(updatePositionRunnable);
- seekbar.setProgress(song.getCurrentPosition());
- handler.postDelayed(updatePositionRunnable, 2000);
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- song.stop();
- song.release();
- }
- sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- boolean userTouch;
- @Override
- public void onStopTrackingTouch(SeekBar arg0) {
- userTouch = false;
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0) {
- userTouch = true;
- }
- @Override
- public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
- if(mPlayer.isPlaying() && arg2)
- mPlayer.seekTo(arg1);
- }
- });
Add Comment
Please, Sign In to add comment