Selamat sore
sobat, langsung saja disini saya akan mensshare cara membuat program pemutar
music untuk android menggunakan eclipse.
Langsung saja desain layout seperti
ini;
Nah setelah itu ketikan list coding
ini di .XML:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="4px"
>
<ImageButton android:id="@+id/play"
android:src="@drawable/play"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingRight="4px"
android:enabled="false"
/>
<TextView
android:text="Play"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="4px"
>
<ImageButton android:id="@+id/pause"
android:src="@drawable/pause"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingRight="4px"
/>
<TextView
android:text="Pause"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="4px"
>
<ImageButton android:id="@+id/stop"
android:src="@drawable/stop"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingRight="4px"
/>
<TextView
android:text="Stop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
</LinearLayout>
Nah setelah itu ketikan list coding
ini di .JAVA:
package com.wilis.music;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import
android.widget.ImageButton;
public class music extends Activity
implements
MediaPlayer.OnCompletionListener {
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private MediaPlayer mp;
@Override
public void onCreate(Bundle
icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play=(ImageButton)findViewById(R.id.play);
pause=(ImageButton)findViewById(R.id.pause);
stop=(ImageButton)findViewById(R.id.stop);
play.setOnClickListener(new
View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new
View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new
View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
setup();
}
@Override
public void onDestroy() {
super.onDestroy();
if (stop.isEnabled()) {
stop();
}
}
public void onCompletion(MediaPlayer
mp) {
stop();
}
private void play() {
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
private void stop() {
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);
try {
mp.prepare();
mp.seekTo(0);
play.setEnabled(true);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void pause() {
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.himne_barcelona);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}
private void goBlooey(Throwable
t) {
AlertDialog.Builder
builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
}
Setelah itu coba jalan kan hasil
ketikan program sobat tadi apakah hasil nya seperti ini: