Advertisement
euis_kusesa

MainActivity.java

Dec 27th, 2018
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package com.aplysit.latihanmediaplayer;
  2.  
  3. import android.media.MediaPlayer;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.     Button playmusic;
  11.     Button playagust;
  12.     Button playgugur;
  13.     Button playhalo;
  14.     Button playgaruda;
  15.     Button playpemuda;
  16.  
  17.     MediaPlayer play;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         playmusic = (Button) findViewById(R.id.indonesia);
  25.         playagust = (Button) findViewById(R.id.merdeka);
  26.         playgugur = (Button) findViewById(R.id.gugur);
  27.         playhalo = (Button) findViewById(R.id.halo_bandung);
  28.         playgaruda = (Button) findViewById(R.id.garuda);
  29.         playpemuda = (Button) findViewById(R.id.pemuda);
  30.  
  31.         playmusic.setOnClickListener(new View.OnClickListener() {
  32.             @Override
  33.             public void onClick(View v) {
  34.                 playSound(1);
  35.             }
  36.         });
  37.         playagust.setOnClickListener(new View.OnClickListener() {
  38.             @Override
  39.             public void onClick(View v) {
  40.                 playSound(2);
  41.             }
  42.         });
  43.         playgugur.setOnClickListener(new View.OnClickListener() {
  44.             @Override
  45.             public void onClick(View v) {
  46.                 playSound(3);
  47.             }
  48.         });
  49.         playhalo.setOnClickListener(new View.OnClickListener() {
  50.             @Override
  51.             public void onClick(View v) {
  52.                 playSound(4);
  53.             }
  54.         });
  55.         playgaruda.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View v) {
  58.                 playSound(5);
  59.             }
  60.         });
  61.         playpemuda.setOnClickListener(new View.OnClickListener() {
  62.             @Override
  63.             public void onClick(View v) {
  64.                 playSound(6);
  65.             }
  66.         });
  67.  
  68.  
  69.     }
  70.     @Override
  71.     protected void onStop() {
  72.         super.onStop();
  73.         if (this.isFinishing()){
  74.             play.stop();
  75.         }
  76.     }
  77.  
  78.     private void playSound(int arg){
  79.  
  80.         try{
  81.             if (play.isPlaying()){
  82.                 play.stop();
  83.                 play.release();
  84.             }
  85.         }catch (Exception e){
  86.  
  87.         }
  88.  
  89.         if (arg ==1){
  90.             play = MediaPlayer.create(this, R.raw.indonesia);
  91.         }else if (arg ==2){
  92.             play = MediaPlayer.create(this, R.raw.merdeka);
  93.         }else if (arg ==3){
  94.             play = MediaPlayer.create(this, R.raw.gugur);
  95.         }else if (arg ==4){
  96.             play = MediaPlayer.create(this, R.raw.halo_bandung);
  97.         }else if (arg ==5){
  98.             play = MediaPlayer.create(this, R.raw.garuda);
  99.         }else if (arg ==6){
  100.             play = MediaPlayer.create(this, R.raw.pemuda_pemudi);
  101.         }
  102.  
  103.         play.setLooping(true);
  104.         play.start();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement