Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aplysit.latihanmediaplayer;
- import android.media.MediaPlayer;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- Button playmusic;
- Button playagust;
- Button playgugur;
- Button playhalo;
- Button playgaruda;
- Button playpemuda;
- MediaPlayer play;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- playmusic = (Button) findViewById(R.id.indonesia);
- playagust = (Button) findViewById(R.id.merdeka);
- playgugur = (Button) findViewById(R.id.gugur);
- playhalo = (Button) findViewById(R.id.halo_bandung);
- playgaruda = (Button) findViewById(R.id.garuda);
- playpemuda = (Button) findViewById(R.id.pemuda);
- playmusic.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- playSound(1);
- }
- });
- playagust.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- playSound(2);
- }
- });
- playgugur.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- playSound(3);
- }
- });
- playhalo.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- playSound(4);
- }
- });
- playgaruda.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- playSound(5);
- }
- });
- playpemuda.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- playSound(6);
- }
- });
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (this.isFinishing()){
- play.stop();
- }
- }
- private void playSound(int arg){
- try{
- if (play.isPlaying()){
- play.stop();
- play.release();
- }
- }catch (Exception e){
- }
- if (arg ==1){
- play = MediaPlayer.create(this, R.raw.indonesia);
- }else if (arg ==2){
- play = MediaPlayer.create(this, R.raw.merdeka);
- }else if (arg ==3){
- play = MediaPlayer.create(this, R.raw.gugur);
- }else if (arg ==4){
- play = MediaPlayer.create(this, R.raw.halo_bandung);
- }else if (arg ==5){
- play = MediaPlayer.create(this, R.raw.garuda);
- }else if (arg ==6){
- play = MediaPlayer.create(this, R.raw.pemuda_pemudi);
- }
- play.setLooping(true);
- play.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement