Posted by : Unknown
Sabtu, 09 Desember 2017
Seting sperti berikut
Letakan file audio di folder
res - raw
res - raw
Dan di file MainActivity.java isi dengan script ini
package com.melki.droid;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
/** Called when the activity is first created. */
private Button btnPlay;
private Button btnPause;
private Button btnStop;
private MediaPlayer mp;
private Button btnPause;
private Button btnStop;
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = new MediaPlayer();
btnPlay = (Button) findViewById(R.id.btnPLAY);
btnPause = (Button) findViewById(R.id.btnPAUSE);
btnStop = (Button) findViewById(R.id.btnSTOP);
btnPause = (Button) findViewById(R.id.btnPAUSE);
btnStop = (Button) findViewById(R.id.btnSTOP);
stateAwal();
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
play();
btnPlay.setEnabled(false);
btnPause.setEnabled(true);
btnStop.setEnabled(true);
}
});
@Override
public void onClick(View v) {
play();
btnPlay.setEnabled(false);
btnPause.setEnabled(true);
btnStop.setEnabled(true);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pause();
}
});
@Override
public void onClick(View v) {
pause();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
}
@Override
public void onClick(View v) {
stop();
}
});
}
/** State Awal / Pertama Dijalankan */
public void stateAwal(){
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
btnStop.setEnabled(false);
}
public void stateAwal(){
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
btnStop.setEnabled(false);
}
/** Dijalankan Oleh Tombol Play */
private void play() {
/** Memanggil File MP3 "indonesiaraya.mp3" */
mp = MediaPlayer.create(this, R.raw.gara);
private void play() {
/** Memanggil File MP3 "indonesiaraya.mp3" */
mp = MediaPlayer.create(this, R.raw.gara);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/** Menjalankan Audio */
mp.start();
mp.start();
/** Penanganan Ketika Suara Berakhir */
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stateAwal();
}
});
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stateAwal();
}
});
}
/** Dijalankan Oleh Tombol Pause */
public void pause(){
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
public void pause(){
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
}
} else {
if(mp!=null){
mp.start();
} else {
if(mp!=null){
mp.start();
}
}
}
}
}
/** Dijalankan Oleh Tombol Stop */
public void stop(){
mp.stop();
public void stop(){
mp.stop();
try{
mp.prepare();
mp.seekTo(0);
}catch (Throwable t) {
t.printStackTrace();
}
mp.prepare();
mp.seekTo(0);
}catch (Throwable t) {
t.printStackTrace();
}
stateAwal();
}
}
}
}
Untuk file main.xml isi dengan script ini
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Lagu Indonesia Raya"
android:id="@+id/textView"
android:layout_marginTop="59dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Lagu Indonesia Raya"
android:id="@+id/textView"
android:layout_marginTop="59dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="@+id/btnPLAY"
android:layout_marginTop="102dp"
android:layout_below="@+id/textView"
android:layout_toLeftOf="@+id/btnPAUSE"
android:layout_toStartOf="@+id/btnPAUSE"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="@+id/btnPLAY"
android:layout_marginTop="102dp"
android:layout_below="@+id/textView"
android:layout_toLeftOf="@+id/btnPAUSE"
android:layout_toStartOf="@+id/btnPAUSE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:id="@+id/btnSTOP"
android:layout_alignTop="@+id/btnPAUSE"
android:layout_toRightOf="@+id/btnPAUSE"
android:layout_toEndOf="@+id/btnPAUSE"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:id="@+id/btnSTOP"
android:layout_alignTop="@+id/btnPAUSE"
android:layout_toRightOf="@+id/btnPAUSE"
android:layout_toEndOf="@+id/btnPAUSE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PAUSE"
android:id="@+id/btnPAUSE"
android:layout_alignTop="@+id/btnPLAY"
android:layout_centerHorizontal="true"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PAUSE"
android:id="@+id/btnPAUSE"
android:layout_alignTop="@+id/btnPLAY"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="by okedroid.com"
android:id="@+id/textView2"
android:layout_marginBottom="106dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="by okedroid.com"
android:id="@+id/textView2"
android:layout_marginBottom="106dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>