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