Sunday, 20 September 2015

Source Code cara Bermain Audio di Android



/*Code java : */

package com.example.bermainaudio;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class Audio_Activity extends ActionBarActivity {
      ImageButton mainkan;
      TextView keterangan;
      MediaPlayer mp;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_audio_);
           
            keterangan=(TextView)findViewById(R.id.ket);
            keterangan.setText("Silakan klik tombol play");
            mainkan=(ImageButton)findViewById(R.id.BermainMusik);
            mainkan.setOnClickListener(new OnClickListener(){
                  public void onClick(View arg0){
                        mainkan.setEnabled(false);
                        keterangan.setText("Tombol play tidak aktif");
                        go();
                        }
                  });
            }
      public void go(){
            mp=MediaPlayer.create(Audio_Activity.this, R.raw.intro); try {
            mp.prepare(); } catch (IllegalStateException e) {
                  // Auto-generated catch block
                  e.printStackTrace(); }
                  catch (IOException e) {
                        // Auto-generated catch block
                  e.printStackTrace(); }
                  mp.start();
                  mp.setOnCompletionListener(new OnCompletionListener(){
                        public void onCompletion(MediaPlayer arg0){
                              mainkan.setEnabled(true);
                              keterangan.setText("Silakan klik tombol play"); }
                        });
                  }
                       
           
     

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.audio_, menu);
            return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
                      
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                  return true;
            }
            return super.onOptionsItemSelected(item);
      }
}


/*code 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:textSize="15px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/textView"
     android:text="Memainkan Musik">
    
 </TextView>
 <ImageButton
     android:id="@+id/BermainMusik"
     android:layout_height="wrap_content"
     android:adjustViewBounds="false"
     android:src="@drawable/play"
     android:layout_gravity="center_vertical|center_horizontal"
     android:layout_width="fill_parent">
    
 </ImageButton>
 <TextView
     android:text=""
     android:id="@+id/ket"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center_horizontal"
     android:textSize="15px">
    
 </TextView>
 </LinearLayout>

0 comments:

Post a Comment