Android Soundboard不会播放声音

时间:2011-07-28 19:24:26

标签: android

我创建了一个音板,每次我点击按钮播放某个声音都没有任何反应。这是声音管理器代码:

package com.androidbook.ufgsoundboard;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;



public class SoundManager extends Activity {

    private  SoundPool mSoundPool; 
    private  HashMap<Integer, Integer> mSoundPoolMap; 
    private  AudioManager  mAudioManager;
    private  Context mContext;


    public SoundManager()
    {

    }

    public void initSounds(Context theContext) { 
         mContext = theContext;
         mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
         mSoundPoolMap = new HashMap<Integer, Integer>(); 
         mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
    } 

    public void addSound(int Index,int SoundID)
    {
        mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

    }

    public void playSound(int index) { 

         int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
         mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    }

}

这是音板代码:

package com.androidbook.ufgsoundboard;

import com.androidbook.ufgsoundboard.SoundManager;


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

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.petera);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.sound1);
        mSoundManager.addSound(2, R.raw.sound2);
        mSoundManager.addSound(3, R.raw.sound3);
        mSoundManager.addSound(5, R.raw.sound5);
        mSoundManager.addSound(7, R.raw.sound7);
        mSoundManager.addSound(9, R.raw.sound9);
        mSoundManager.addSound(10, R.raw.sound10);
        mSoundManager.addSound(11, R.raw.sound11);
        mSoundManager.addSound(13, R.raw.sound13);
        mSoundManager.addSound(18, R.raw.sound18);
        mSoundManager.addSound(19, R.raw.sound19);
        mSoundManager.addSound(20, R.raw.sound20);
        mSoundManager.addSound(21, R.raw.sound21);
        mSoundManager.addSound(22, R.raw.sound22);
        mSoundManager.addSound(23, R.raw.sound23);
        mSoundManager.addSound(24, R.raw.sound24);
        mSoundManager.addSound(28, R.raw.sound28);
        mSoundManager.addSound(30, R.raw.sound30);



        Button SoundButton1 = (Button)findViewById(R.id.button3);
        SoundButton1.setOnClickListener(new OnClickListener() {

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

    }
});       
        Button SoundButton2 = (Button)findViewById(R.id.button4);
        SoundButton2.setOnClickListener(new OnClickListener() {

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

    }
});    
        Button SoundButton3 = (Button)findViewById(R.id.button5);
        SoundButton3.setOnClickListener(new OnClickListener() {

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

    }
});    
        Button SoundButton4 = (Button)findViewById(R.id.button6);
        SoundButton4.setOnClickListener(new OnClickListener() {

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

    }
});    
        Button SoundButton5 = (Button)findViewById(R.id.button7);
        SoundButton5.setOnClickListener(new OnClickListener() {

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

    }
});   
        Button SoundButton6 = (Button)findViewById(R.id.button8);
        SoundButton6.setOnClickListener(new OnClickListener() {

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

    }
});    
        Button SoundButton7 = (Button)findViewById(R.id.button9);
        SoundButton7.setOnClickListener(new OnClickListener() {

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

    }
});
  Button SoundButton8 = (Button)findViewById(R.id.button10);
       SoundButton8.setOnClickListener(new OnClickListener() {

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

    }
 });    
        Button SoundButton9 = (Button)findViewById(R.id.button11);
        SoundButton9.setOnClickListener(new OnClickListener() {

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

    }
});    
       Button SoundButton10 = (Button)findViewById(R.id.button12);
       SoundButton10.setOnClickListener(new OnClickListener() {

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

    }
 });    
        Button SoundButton11 = (Button)findViewById(R.id.button13);
        SoundButton11.setOnClickListener(new OnClickListener() {

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

    }
});    
        Button SoundButton12 = (Button)findViewById(R.id.button14);
        SoundButton12.setOnClickListener(new OnClickListener() {

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

    }
});    
       Button SoundButton13 = (Button)findViewById(R.id.button15);
       SoundButton13.setOnClickListener(new OnClickListener() {

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

    }
 });    
        Button SoundButton14 = (Button)findViewById(R.id.button16);
        SoundButton14.setOnClickListener(new OnClickListener() {

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

    }
});    

        Button SoundButton15 = (Button)findViewById(R.id.button17);
                SoundButton15.setOnClickListener(new OnClickListener() {

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

            }
        });
                Button SoundButton16 = (Button)findViewById(R.id.button18);
                SoundButton16.setOnClickListener(new OnClickListener() {

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

            }
        });    
                Button SoundButton17 = (Button)findViewById(R.id.button19);
                SoundButton17.setOnClickListener(new OnClickListener() {

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

            }
        });    
                Button SoundButton18 = (Button)findViewById(R.id.button20);
                SoundButton18.setOnClickListener(new OnClickListener() {

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

            }

                });
        }};

我无法解决为什么它不会加载。按下时按钮突出显示,但没有声音。我错过了一些明显的东西吗?

由于

1 个答案:

答案 0 :(得分:0)

您的代码非常复杂,请使用此代替它对我有用!! 这是播放声音的单个按钮的示例

 package com.example.soundtest;

 import android.app.Activity;
 import android.media.SoundPool;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

 Button white;
 SoundPool spool;
 int soundID;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    soundID = spool.load(this, R.raw.anysoundfile, 1);

    white = (Button)findViewById(R.id.button1);
    white.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Sound();
        }
    });
}

public void Sound(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    spool.play(soundID, volume, volume, 1, 0, 1f);

};
}