-2

我有点麻烦,这里是gen。想法,我创建了一个简单的类似游戏的应用程序.. 但是问题是当我尝试单击标题屏幕中的按钮时,它不会播放单击声音.. 任何人都可以帮助我.. 这是我的声音代码

    public class Effects extends Activity implements MediaPlayer.OnCompletionListener {

        Button mPlay;
        MediaPlayer mPlayer;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mPlay = (Button)findViewById(R.id.btnStart);
            mPlay.setOnClickListener(playListener);

            setContentView(R.layout.activity_main);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            if(mPlayer != null) {
                mPlayer.release();
            }
        }

        private View.OnClickListener playListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(mPlayer == null) {
                    try {
                        mPlayer = MediaPlayer.create(Effects.this, R.raw.button11);
                        mPlayer.start();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    mPlayer.stop();
                    mPlayer.release();
                    mPlayer = null;
                }
            }
        };
        @Override
        public void onCompletion(MediaPlayer arg0) {
            // TODO Auto-generated method stub

        }
    }    

这是我希望在单击此按钮时播放声音的主 java 代码

public class Main extends Activity implements OnClickListener
{
Button about;
Button Quit;
Button start;
protected void onCreate(Bundle onSavedInstanceState) {
    super.onCreate(onSavedInstanceState);
    setContentView(R.layout.activity_main);
    about = (Button)findViewById(R.id.btnAbout);
    about.setOnClickListener(this);
    Quit = (Button)findViewById(R.id.btnQuit);
    Quit.setOnClickListener(this);
    start = (Button)findViewById(R.id.btnStart);
    start.setOnClickListener(this);

}
public void onClick(View argo)
{
    if(argo.getId()==R.id.btnAbout)
    {
        Intent i = new Intent(this,About.class);
        this.startActivity(i);
    }
    if(argo.getId()==R.id.btnQuit)
    {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    if(argo.getId()==R.id.btnStart)
    {
        Intent start = new Intent(this,Howto.class);
        this.startActivity(start);
        Intent svc=new Intent(this,Effects.class);
        startService(svc);
    }
    }
}
4

1 回答 1

0

用于SoundPool播放您的声音: http: //developer.android.com/reference/android/media/SoundPool.html您应该在您的声音中初始化声音onCreate(),然后在其中播放它们onClick()

而不是做成千上万毫无意义的事情if(argo.getId()==R.id.btnQuit)(你至少应该使用else)熟悉switch/case语法)

于 2013-04-07T11:10:19.077 回答