Android:为什么我的按钮没有播放其音效?

时间:2011-03-02 21:28:22

标签: java android audio

第一个按钮工作正常,但当我点击第二个按钮时,没有任何反应。我没有收到任何错误,我看不出有什么问题。

Sounds.java

package com.andrew.finnandjake;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Sounds extends Activity {
private SoundManager mSoundManager;

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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());
    mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);

    Button b1 = (Button)findViewById(R.id.Button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mSoundManager.playSound(1);


        }
    });
}
public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);

        Button b2 = (Button)findViewById(R.id.Button2);
        b2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(2);
}
});
    }
}

2 个答案:

答案 0 :(得分:2)

您不能随意创建与onCreate1一样的方法,它永远不会由您或Activity执行。

这就是你所需要的:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   mSoundManager = new SoundManager();
   mSoundManager.initSounds(this);
   mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
   mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);

   Button b1 = (Button)findViewById(R.id.Button1);
   b1.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mSoundManager.playSound(1);
      }
   });

   Button b2 = (Button)findViewById(R.id.Button2);
   b2.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mSoundManager.playSound(2);
      }
   });
}

答案 1 :(得分:1)

您没有在实际的OnCreate中分配onclick侦听器。这应该有效:

package com.andrew.finnandjake;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Sounds extends Activity {
private SoundManager mSoundManager;

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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());
    mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
    mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);

    Button b1 = (Button)findViewById(R.id.Button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mSoundManager.playSound(1);


        }
    });

    Button b2 = (Button)findViewById(R.id.Button2);
    b2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mSoundManager.playSound(2);
        }
    });
}