2. Ganti file activity_main.xml dengan skrip 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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/aaa" tools:context=".MainActivity">
<Button android:id="@+id/btnPlay" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btnPause" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:drawableLeft="@android:drawable/ic_media_play" android:onClick="playSong" android:text="Play" />
<Button android:id="@+id/btnPause" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btnStop" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:drawableLeft="@android:drawable/ic_media_pause" android:onClick="pauseSong" android:text="Pause" />
<Button android:id="@+id/btnStop" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:drawableLeft="@android:drawable/ic_lock_power_off" android:onClick="stopSong" android:text="Stop" />
<TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:text="Audio Player" android:textAlignment="center" android:textColor="@android:color/background_light" android:textSize="24sp" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btnPlay" android:layout_centerHorizontal="true" android:layout_marginBottom="140dp" android:text="DANIE" android:textColor="@android:color/background_light" android:textSize="70sp" />
</RelativeLayout>
3. Ganti file MainActivity.java dengan skrip berikut :
package com.example.hp.audioplayer; import android.media.MediaPlayer; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mp=MediaPlayer.create(this, R.raw.adele); } public void playSong(View v){ mp.start(); } public void pauseSong(View v) { mp.pause(); } public void stopSong(View v) { mp.stop(); // mp=MediaPlayer.create(this, R.raw.adele); finish(); moveTaskToBack(true); } }
4. Jalankan aplikasi tersebut.
Comments