Program utama kita letakkan di MainActivity
MainActivity.java
package com.amijaya.androidplaymp3raw;
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.widget.TextView;
// http://cariprogram.blogspot.com
// nuramijaya@gmail.com
public class MainActivity extends Activity {
Button Button01;
TextView TextView01;
MediaPlayer mp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView01 = (TextView) findViewById(R.id.TextView01);
Button01 = (Button) findViewById(R.id.Button01);
Button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button01.setEnabled(false);
TextView01.setText("Playing");
go();
}
});
}
public void go() {
mp = MediaPlayer.create(MainActivity.this, R.raw.heavenearth);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
Button01.setEnabled(true);
TextView01.setText("Play");
}
});
}
}
Kemudian tampilannya kita pakai saja tampilan default kemudian ditambah satu buah Button dengan nama Button01 dan satu buah TextView dengan nama TextView01 :
main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play MP3"
/>
<Button android:text="Play" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:text="Klik Play" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
Android Manifest tidak perlu kita ubah, sesuai dengan Activity yang telah dibuat dalam hal ini MainActivity.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amijaya.androidplaymp3raw"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Hasilnya
Project selengkapnya silakan didownload disini :
https://docs.google.com/open?id=0B4i1FYc_4RXzTndCazBjaVA4RDA
Semoga bermanfaat, Selamat Coding.