Musik Player
1. untuk activity_main untuk tab labelnya tuliskan scrip di bawah ini
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
/>
</LinearLayout>
</TabHost>
2. desain activity_lagu1 untuk tampilan pemutar musik tuliskan scrip di bawah ini
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
/>
</LinearLayout>
</TabHost>
2. desain activity_lagu1 untuk tampilan pemutar musik tuliskan scrip di bawah ini
<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"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Lagu1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:textStyle="bold"
android:text="Gorontalo" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo"
android:layout_marginTop="30dp"
/>
<Button
android:id="@+id/putar"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="370dp"
android:background="@drawable/play"
android:text="putar" />
<Button
android:id="@+id/pause"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="370dp"
android:layout_toRightOf="@+id/putar"
android:background="@drawable/pause"
android:text="jeda" />
<Button
android:id="@+id/stop"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignLeft="@+id/putar"
android:layout_marginLeft="190dp"
android:layout_toRightOf="@+id/pause"
android:layout_marginTop="370dp"
android:background="@drawable/stop"
android:text="stop" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Lagu1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:textStyle="bold"
android:text="Gorontalo" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo"
android:layout_marginTop="30dp"
/>
<Button
android:id="@+id/putar"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="370dp"
android:background="@drawable/play"
android:text="putar" />
<Button
android:id="@+id/pause"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="370dp"
android:layout_toRightOf="@+id/putar"
android:background="@drawable/pause"
android:text="jeda" />
<Button
android:id="@+id/stop"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignLeft="@+id/putar"
android:layout_marginLeft="190dp"
android:layout_toRightOf="@+id/pause"
android:layout_marginTop="370dp"
android:background="@drawable/stop"
android:text="stop" />
</RelativeLayout>
3.kemudian desain untuk album.xml
<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"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AlbumActivity" >
<Button
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo"
android:layout_marginTop="30dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Album"
android:layout_below="@+id/logo"
android:gravity="center"
android:textStyle="bold"
/>
</RelativeLayout>
4. artis.xml
<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"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ArtisActivity" >
<Button
android:id="@+id/artis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/artis"
android:layout_marginLeft="80dp"
android:gravity="center"
android:layout_marginTop="30dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Artis"
android:layout_below="@+id/artis"
android:gravity="center"
android:textStyle="bold"
/>
</RelativeLayout>
5. lagu.xml yaitu untuk pilihan lagu
<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"
android:orientation="vertical"
android:background="@drawable/bg">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="30dp"
android:textStyle="bold"
android:text="LAGU GORONTALO" />
<Button
android:id="@+id/lagu1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gorontalo"
android:textStyle="bold"
android:layout_marginTop="80dp"
/>
<Button
android:id="@+id/lagu2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hilawo"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below="@+id/lagu1"
/>
<Button
android:id="@+id/lagu3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Torabua"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below="@+id/lagu2"
/>
<Button
android:id="@+id/lagu4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sababu"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below="@+id/lagu3"
/>
<Button
android:id="@+id/lagu5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lunggabongo"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below="@+id/lagu4"
/>
</RelativeLayout>
6. lagu1.xml untuk pilihan lagu yang akan di putar
<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"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Lagu2" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:textStyle="bold"
android:text="Hilawo" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo"
android:layout_marginTop="30dp"
/>
<Button
android:id="@+id/putar"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="370dp"
android:background="@drawable/play"
android:text="putar" />
<Button
android:id="@+id/pause"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="370dp"
android:layout_toRightOf="@+id/putar"
android:background="@drawable/pause"
android:text="jeda" />
<Button>
langkah selanjutnya kita akan membuat java nya agar file xml tadi dapat di jalannkan yang pertama kita buka pada file src dan buatkan class yang akan kita gunakan.
1. MainActivity.java
package com.titin.mediaplayer;
import android.os.Bundle;
import android.widget.TabHost;
import android.app.TabActivity;
import android.content.Intent;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabhost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, AlbumActivity.class);//content pada tab yang akan kita buat
spec = tabhost.newTabSpec("album").setIndicator("Album",null).setContent(intent);//mengeset nama tab dan mengisi content pada menu tab anda.
tabhost.addTab(spec);//untuk membuat tabbaru disini bisa diatur sesuai keinginan anda
intent = new Intent().setClass(this, LaguActivity.class);
spec = tabhost.newTabSpec("lagu").setIndicator("Lagu",null).setContent(intent);
tabhost.addTab(spec);
intent = new Intent().setClass(this, ArtisActivity.class);
spec = tabhost.newTabSpec("artis").setIndicator("Artis",null).setContent(intent);
tabhost.addTab(spec);
}
}
2. LaguActivity.java
package com.titin.mediaplayer;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.animation.ArgbEvaluator;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController.MediaPlayerControl;
public class LaguActivity extends Activity implements OnClickListener{
Button lagu1;
Button lagu2;
Button lagu3;
Button lagu4;
Button lagu5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lagu);
Button lagu1 = ( Button)findViewById(R.id.lagu1);
Button lagu2 = ( Button)findViewById(R.id.lagu2);
Button lagu3 = ( Button)findViewById(R.id.lagu3);
Button lagu4 = ( Button)findViewById(R.id.lagu4);
Button lagu5 = ( Button)findViewById(R.id.lagu5);
lagu1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (LaguActivity. this, Lagu1.class);
startActivity(masuk);
}
});
lagu2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (LaguActivity. this, Lagu2.class);
startActivity(masuk);
}
});
lagu3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (LaguActivity. this, Lagu3.class);
startActivity(masuk);
}
});
lagu4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (LaguActivity. this, Lagu4.class);
startActivity(masuk);
}
});
lagu5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (LaguActivity. this, Lagu5.class);
startActivity(masuk);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lagu, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
3. ArtisActivity.java
package com.titin.mediaplayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArtisActivity extends Activity implements OnClickListener{
Button artis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artis);
Button artis = (Button)findViewById(R.id.artis);
artis.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (ArtisActivity. this, LaguActivity.class);
startActivity(masuk);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.artis, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
package com.titin.mediaplayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArtisActivity extends Activity implements OnClickListener{
Button artis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artis);
Button artis = (Button)findViewById(R.id.artis);
artis.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (ArtisActivity. this, LaguActivity.class);
startActivity(masuk);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.artis, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
4. AlbumActivity.java
package com.titin.mediaplayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AlbumActivity extends Activity implements OnClickListener{
Button logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
Button logo = (Button)findViewById(R.id.logo);
logo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (AlbumActivity. this, LaguActivity.class);
startActivity(masuk)
;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.album, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
package com.titin.mediaplayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AlbumActivity extends Activity implements OnClickListener{
Button logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
Button logo = (Button)findViewById(R.id.logo);
logo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent (AlbumActivity. this, LaguActivity.class);
startActivity(masuk)
;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.album, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
5.Lagu1.java
package com.titin.mediaplayer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Lagu1 extends Activity implements MediaPlayer.OnCompletionListener{
private Button play;
private Button jeda;
private Button berhenti;
private Button exit;
private MediaPlayer mp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_lagu1);
play=(Button)findViewById(R.id.putar);
jeda=(Button)findViewById(R.id.pause);
berhenti=(Button)findViewById(R.id.stop);
berhenti.setOnClickListener (new View.OnClickListener () {
public void onClick(View view){
stop();
}
});
// fungsi tobol play
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
// fungsi tobol pause
jeda.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
setup();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void play() {
mp.start();
jeda.setEnabled(true);
play.setEnabled(false);
berhenti.setEnabled(true);
}
// fungsi tobol stop
private void stop() {
mp.stop();
play.setEnabled(true);
jeda.setEnabled(false);
try {
mp.prepare();
mp.seekTo(0);
jeda.setEnabled(false);
play.setEnabled(true);
berhenti.setEnabled(false);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void pause() {
mp.pause();
play.setEnabled(true);
berhenti.setEnabled(true);
jeda.setEnabled(false);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.gtlo);
}
catch (Throwable t) {
goBlooey(t);
}
};
private void loadClip1() {
try {
mp=MediaPlayer.create(this, R.raw.gtlo);
}
catch (Throwable t) {
goBlooey(t);
}
};
private void setup() {
loadClip();
jeda.setEnabled(false);
play.setEnabled(true);
berhenti.setEnabled(false);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
}
package com.titin.mediaplayer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Lagu1 extends Activity implements MediaPlayer.OnCompletionListener{
private Button play;
private Button jeda;
private Button berhenti;
private Button exit;
private MediaPlayer mp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_lagu1);
play=(Button)findViewById(R.id.putar);
jeda=(Button)findViewById(R.id.pause);
berhenti=(Button)findViewById(R.id.stop);
berhenti.setOnClickListener (new View.OnClickListener () {
public void onClick(View view){
stop();
}
});
// fungsi tobol play
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
// fungsi tobol pause
jeda.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
setup();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void play() {
mp.start();
jeda.setEnabled(true);
play.setEnabled(false);
berhenti.setEnabled(true);
}
// fungsi tobol stop
private void stop() {
mp.stop();
play.setEnabled(true);
jeda.setEnabled(false);
try {
mp.prepare();
mp.seekTo(0);
jeda.setEnabled(false);
play.setEnabled(true);
berhenti.setEnabled(false);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void pause() {
mp.pause();
play.setEnabled(true);
berhenti.setEnabled(true);
jeda.setEnabled(false);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.gtlo);
}
catch (Throwable t) {
goBlooey(t);
}
};
private void loadClip1() {
try {
mp=MediaPlayer.create(this, R.raw.gtlo);
}
catch (Throwable t) {
goBlooey(t);
}
};
private void setup() {
loadClip();
jeda.setEnabled(false);
play.setEnabled(true);
berhenti.setEnabled(false);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
}
setelah itu jika tidak ada yang eror maka jalankan aplikasinya !!
untuk tampilan awal terdiri atas album, artis dan lagu
kemudian untuk tampilan pemutar musik ada button play, jeda dan stop untuk penyimpanan gambar buka pada file res > drawble..
Komentar
Posting Komentar