Pemograman
Perangkat Bergerak
JOB Ke 8
oleh:
SILVIA NADIVA
Akademi
Komunitas Negeri Padang Pariaman
Fakultas
Teknik
Universitas
Negeri Padang
Teknik
Elektronika
2017
Langkah-langkah:
1.
Buat project baru dengan nama MusikSilvia
2. Buka activity_main.xml di res ->
layout -> activity_main.xml
Buat 1 folder di dalam
res , beri nama folder raw dan beri satu lagu yang bernama utpi
Buat 2 Button , dengan
id , btnPlay & btnPause
3. Sesuaikan main.xml seperti berikut:
<?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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY STOP" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="58dp"
android:layout_marginTop="139dp"
android:text="Stop" />
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnStop"
android:layout_alignBottom="@+id/btnStop"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/btnStop"
android:text="Play" />
</RelativeLayout>
<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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY STOP" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="58dp"
android:layout_marginTop="139dp"
android:text="Stop" />
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnStop"
android:layout_alignBottom="@+id/btnStop"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/btnStop"
android:text="Play" />
</RelativeLayout>
3. Lalu buka MusikSilvia1Activity.java isikan code
seperti berikut:
package musik.silvia;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MusikSilvia1Activity extends Activity {
MediaPlayer mp;
Button btnPlay;
Button btnStop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
btnPlay.setEnabled(false);
btnStop.setEnabled(true);
go();
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
stop();
}
});
}
private void go() {
mp = MediaPlayer.create(MusikSilvia1Activity.this, R.raw.asalbahagia);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
}
});
}
public void stop(){
mp.stop();
}
}
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MusikSilvia1Activity extends Activity {
MediaPlayer mp;
Button btnPlay;
Button btnStop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
btnPlay.setEnabled(false);
btnStop.setEnabled(true);
go();
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
stop();
}
});
}
private void go() {
mp = MediaPlayer.create(MusikSilvia1Activity.this, R.raw.asalbahagia);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
}
});
}
public void stop(){
mp.stop();
}
}
4.
Jalankan!
TUGAS
Perbaiki program PlayStop di atas agar
tampilannya lebih bagus dengan mengganti penggunaan tombol dengan image button,
sehingga tombolnya tidak perlu menggunakan text, akan tetapi cukup dengan
button yang memiliki gambar play dan stop!
2. Sesuaikan main.xml sebagai berikut:
<?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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY STOP" />
<ImageButton
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="58dp"
android:layout_marginTop="139dp"
android:text="Stop" />
<ImageButton
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnStop"
android:layout_alignBottom="@+id/btnStop"
android:layout_marginLeft="22dp"
android:src="@drawable/ic_launcher"
android:layout_toRightOf="@+id/btnStop"
android:text="Play" />
</RelativeLayout>
<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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY STOP" />
<ImageButton
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="58dp"
android:layout_marginTop="139dp"
android:text="Stop" />
<ImageButton
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnStop"
android:layout_alignBottom="@+id/btnStop"
android:layout_marginLeft="22dp"
android:src="@drawable/ic_launcher"
android:layout_toRightOf="@+id/btnStop"
android:text="Play" />
</RelativeLayout>
3.Kemudian sesuaikan TugasMusikSilviaActivity.java sebagai berikut:
package tugas.musik.silvia;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TugasMusikSilviaActivity extends Activity {
MediaPlayer mp;
Button btnPlay;
Button btnStop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
btnPlay.setEnabled(false);
btnStop.setEnabled(true);
go();
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
stop();
}
});
}
private void go() {
mp = MediaPlayer.create(TugasMusikSilviaActivity.this, R.raw.asalbahagia);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
}
});
}
public void stop(){
mp.stop();
}
}
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TugasMusikSilviaActivity extends Activity {
MediaPlayer mp;
Button btnPlay;
Button btnStop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
btnPlay.setEnabled(false);
btnStop.setEnabled(true);
go();
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
stop();
}
});
}
private void go() {
mp = MediaPlayer.create(TugasMusikSilviaActivity.this, R.raw.asalbahagia);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
btnPlay.setEnabled(true);
btnStop.setEnabled(false);
}
});
}
public void stop(){
mp.stop();
}
}
4.Copykan gambar play dan stop ke drawable dan buat sebuah folder raw di res.
Play
Stop
Tidak ada komentar:
Posting Komentar