플래이 버튼을 누르면 음악이 재생되고, 중지를 누르면 음악이 꺼지는 것까지는 되었는데요,

그 화면을 벗어나기 위해 백키를 누르거나 홈키를 누르면 음악이 꺼지게 하고 싶습니다.

다른 화면을 넘어가거나, 백키를 눌러도 음악이 계속 나오더라구요.

(참고로 저는 이 소스를 책을 보고 겨우 작성했습니다. 그래서 답변을 주실 때, 소스까지 정확하게 알려주셔야 해요. 저는 정말 초보입니다.)

 

[ top.java ]_________________________________________________________________________

 

package com.pfo.pw;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class top {

 public Activity act;
 public MediaPlayer mp;

 public top() {

 }

 public top(Activity tmpact) {
  act = tmpact;
 }
 

 public void init() {
  // audio/////////
  mp = MediaPlayer.create(act, R.raw.duegosong_epic);
  try {
   mp.prepare();
  } catch (IllegalStateException e) {
   e.printStackTrace();
  } catch (IOException e) {
   Toast.makeText(act, e.getMessage(), Toast.LENGTH_SHORT).show();
  }
  ImageView playBtn = (ImageView) act.findViewById(R.id.play_btn1);
  playBtn.setOnClickListener(new ImageView.OnClickListener() {
   public void onClick(View v) {
    audioControl();
   }
  });
  ImageView equalBg = (ImageView) act.findViewById(R.id.equal_bg1);
  equalBg.setOnClickListener(new ImageView.OnClickListener() {
   public void onClick(View v) {
    audioInfo();
   }
  });  // audio/////////

 }


 public void audioInfo() {
  AlertDialog dialog = new AlertDialog.Builder(act).create();
  dialog.setTitle(act.getString(R.string.audio_title1));
  dialog.setIcon(act.getResources().getDrawable(R.drawable.logo_5));
  dialog.setButton("확인", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
    return;
   }
  });
  dialog.show();
 }
 public void audioControl() {
  ImageView img = (ImageView) act.findViewById(R.id.equal_bg1);
  final AnimationDrawable equalAnim = (AnimationDrawable) img.getBackground();
  final ImageView playBtn = (ImageView) act.findViewById(R.id.play_btn1);
  if (playBtn.getBackground().getLevel() == 0) {
   playBtn.getBackground().setLevel(1);
   mp.start();
   equalAnim.start();
   mp.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer arg0) {
     playBtn.getBackground().setLevel(0);
     equalAnim.stop();
    }
   });
  } else {
   playBtn.getBackground().setLevel(0);
   mp.pause();
   equalAnim.stop();
  }
 }
 public void backgroundStop() {
  ImageView img = (ImageView) act.findViewById(R.id.equal_bg1);
  AnimationDrawable equalAnim = (AnimationDrawable) img.getBackground();
  ImageView playBtn = (ImageView) act.findViewById(R.id.play_btn1);
  playBtn.getBackground().setLevel(0);
  mp.pause();
  equalAnim.stop();
 }

}

 

 

 

 

[duego.java]_________________________________________________________________________

 

package com.pfo.pw;
import com.pfo.pw.R;
import com.pfo.pw.back;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
 
public class duego extends Activity {

    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.duego);
       
        top top = new top(this);
  top.init();
     
        ImageButton top_arrow_right = (ImageButton)findViewById(R.id.top_arrow_right);
        Animation alphaAnim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        top_arrow_right.startAnimation(alphaAnim);
        top_arrow_right.setOnClickListener(new ImageButton.OnClickListener(){
         public void onClick(View v){
          Intent intent = new Intent(duego.this, back.class);
          startActivity(intent);
          
         }
        });
       
    }
   
}