Berikut adalah activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="" />
<SeekBar
android:id="@+id/sb_status"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seek_bar"
android:src="@android:drawable/ic_media_play" />
<ImageButton
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_alert" />
<ImageButton
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_pause" />
</LinearLayout>
</LinearLayout>
Berikut adalah code pada MainActivity.java
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
SeekBar sb_status;
ImageButton btn_previous, btn_play, btn_pause, btn_stop, btn_next;
MediaPlayer mp;
TextView tv_status;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
btn_previous.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread(new Previous());
thread.start();
}
});
btn_play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread(new Play());
thread.start();
}
});
btn_pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread(new Pause());
thread.start();
}
});
btn_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread(new Stop());
thread.start();
}
});
btn_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread(new Next());
thread.start();
}
});
}
public void onDestroy() {
super.onDestroy();
Thread thread = new Thread(new Stop());
thread.start();
}
public void Init() {
sb_status = (SeekBar) findViewById(R.id.sb_status);
btn_play = (ImageButton) findViewById(R.id.btn_play);
btn_pause = (ImageButton) findViewById(R.id.btn_pause);
btn_stop = (ImageButton) findViewById(R.id.btn_stop);
tv_status = (TextView) findViewById(R.id.tv_status);
mp = MediaPlayer.create(getApplicationContext(), R.raw.song1);
sb_status.setMax(mp.getDuration());
tv_status.setText("Waiting...");
}
class Play implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
tv_status.setText("Playing...");
mp.start();
Thread thread = new Thread(new SeekBarUpdate());
thread.start();
} catch (Exception ex) {
}
}
}
class Pause implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
tv_status.setText("Pause...");
mp.pause();
} catch (Exception ex) {
}
}
}
class Stop implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
tv_status.setText("Waiting...");
mp.stop();
mp.prepare();
mp.seekTo(0);
sb_status.setProgress(mp.getCurrentPosition());
} catch (Exception ex) {
}
}
}
class Next implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
if (mp.isPlaying()) {
Thread threadStop = new Thread(new Stop());
threadStop.start();
}
mp.setDataSource(getApplicationContext(), Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.song2));
mp.prepare();
Thread threadPlay = new Thread(new Play());
threadPlay.start();
} catch (Exception ex) {
}
}
}
class Previous implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
Thread threadStop = new Thread(new Stop());
threadStop.start();
mp.setDataSource(getApplicationContext(), Uri.parse("android.resource://" + getApplicationContext().getPackageName() + R.raw.song1));
mp.prepare();
Thread threadPlay = new Thread(new Play());
threadPlay.start();
} catch (Exception ex) {
}
}
}
class SeekBarUpdate implements Runnable {
public SeekBarUpdate() {
}
@Override
public void run() {
try {
while (mp.getCurrentPosition() <= mp.getDuration()) {
sb_status.setProgress(mp.getCurrentPosition());
Thread.sleep(1000);
}
} catch (Exception ex) {
}
}
}
}
No comments:
Post a Comment