Created
July 10, 2015 08:47
-
-
Save Atcharapon/0f6ccae6c44d0e09b657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.media.MediaPlayer; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.view.Menu; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.widget.Button; | |
import android.widget.SeekBar; | |
import android.widget.TextView; | |
public class MainActivity extends Activity { | |
private MediaPlayer mMedia; | |
private Handler handler = new Handler(); | |
private SeekBar seekBar; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if(mMedia != null){ | |
mMedia.release(); | |
} | |
final TextView txtView = (TextView)findViewById(R.id.textView1); | |
txtView.setText("Source : music.mp3"); | |
/* Resource in R. | |
* mMedia = MediaPlayer.create(this, R.raw.music); | |
* mMedia.start(); | |
*/ | |
/* | |
* from DataSource | |
* mMedia = new MediaPlayer(); | |
* mMedia.setDataSource("http://www.thaicreate.com/music/mymusic.mp3"); | |
* mMedia.start(); | |
* | |
*/ | |
mMedia = MediaPlayer.create(this, R.raw.music); | |
seekBar = (SeekBar)findViewById(R.id.seekBar1); | |
seekBar.setMax(mMedia.getDuration()); | |
seekBar.setOnTouchListener(new OnTouchListener() { | |
public boolean onTouch(View v, MotionEvent event) { | |
UpdateseekChange(v); | |
return false; | |
} | |
}); | |
final Button btn1 = (Button) findViewById(R.id.button1); // Start | |
// Start | |
btn1.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View v) { | |
if (mMedia.isPlaying()) { | |
txtView.setText("Playing : music.mp3...."); | |
mMedia.pause(); | |
} else { | |
txtView.setText("pause : music.mp3...."); | |
mMedia.start(); | |
} | |
} | |
}); | |
} | |
private void UpdateseekChange(View v){ | |
if(mMedia.isPlaying()){ | |
SeekBar sb = (SeekBar)v; | |
mMedia.seekTo(sb.getProgress()); | |
} | |
} | |
public void startPlayProgressUpdater() { | |
seekBar.setProgress(mMedia.getCurrentPosition()); | |
if (mMedia.isPlaying()) { | |
Runnable notification = new Runnable() { | |
public void run() { | |
startPlayProgressUpdater(); | |
} | |
}; | |
handler.postDelayed(notification,1000); | |
} | |
} | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { | |
if(mMedia != null && fromUser){ | |
mMedia.seekTo(progress * 1000); | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
// TODO Auto-generated method stub | |
super.onDestroy(); | |
if(mMedia != null){ | |
mMedia.release(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment