2

我正在为 Android 编写游戏。声音问题:菜单2项(soundOn & soundOff)当您在soundOff声音不出现后按下soundon时

     public class AudioClip {

            float MAX = 500;
            float MIN = 0;

            protected boolean mPlaying = false;
            private boolean mLoop = false;

            public AudioClip(Context ctx, int resID) {
                name = ctx.getResources().getResourceName(resID);

                mPlayer = MediaPlayer.create(ctx, resID);

                    mPlayer.setVolume(MAX, MAX);

                mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    public void onCompletion(MediaPlayer mp) {
                        mPlaying = false;
                        if (ismLoop()) {
                            System.out.println("AudioClip loop " + name);
                            mp.start();
        }
     }

   });
 }


            public synchronized void play() {
                if (mPlaying)
                    return;

                if (mPlayer != null) {
                    mPlaying = true;
                    mPlayer.start();
                }
            }

            public synchronized void stop() {
                try {
                    setmLoop(false);
                    if (mPlaying) {
                        mPlaying = false;
                        mPlayer.pause();
                    }

                } catch (Exception e) {
                    System.err.println("AduioClip::stop " + name + " " + e.toString());
                }
            }

            public synchronized void loop() {
                setmLoop(true);
                mPlaying = true;
                mPlayer.start();

            }

            public void release() {
                if (mPlayer != null) {
                    mPlayer.release();
                    mPlayer = null;
                }
            }
             boolean sound = b;
            public  void setSoundOn(boolean sound)
            {
                this.sound = sound;

            }
            public boolean isSoundOn()
            {
                return sound; 
            }



            public boolean ismLoop() {
                return mLoop;
            }


            public void setmLoop(boolean mLoop) {
                this.mLoop = mLoop;
            }
        }

主类所在的Menu方法如何解决问题

public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = this.getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        SubMenu subMenuSound = menu.addSubMenu("Sound");
        subMenuSound.add(Menu.NONE,IDM_SON,Menu.NONE,"SoundOn");
        subMenuSound.add(Menu.NONE,IDM_SOF,Menu.NONE,"SoundOff");

        sound = menu.add(0, 2, 0, "FullScreenOff");
        this.menu = menu;



        return true;
    }





enter code here @Override
public boolean onOptionsItemSelected(MenuItem item) 
    {


        switch (item.getItemId()) 
        {
        case R.id.new_game:


            LayoutInflater f = LayoutInflater.from(this);
            v = f.inflate(R.layout.main, null);
            setContentView(v);
            v.setFocusable(true);
            v.setFocusableInTouchMode(true);
            break;


        case IDM_SON :

             AudioClip.mPlayer.start();
            break;

        case IDM_SOF : 
            if (AudioClip.mPlayer != null) {
                AudioClip.mPlayer.stop();
            }
            break;

            return false;

    }
}   
4

0 回答 0